WordPress query inside query weird behaviour

34 Views Asked by At

So I'm trying to add card for ad after second post in the loop, it's loading fine but somehow main query is making card for that same post from different post type...

Main query:

<?php 

//query to load selected featured post on front page
$postsPerPage = 12;
$args = array(
    'post_type' => 'post',
    'posts_per_page' => $postsPerPage,
);

$blogPostQuery = new WP_Query($args);

$i = 1;
while($blogPostQuery->have_posts()){
    $blogPostQuery->the_post(); 
    
    if($i == 3) {

     get_template_part('partials/ads', 'post'); 

    }
    
    ?>

    <?php $post_id = get_the_ID(); ?>

    <div class="blogCardBlackOverlay post-<?php echo $post_id; ?>">
        <div class="col-span-1 shadow-2xl">
            <?php echo $i; ?>
        <?php 
            $thumb = get_the_post_thumbnail_url(); 
            
        ?>
            <div class="relative blogPostCard rounded-xl" style="background-image: linear-gradient(rgba(66,32,6,0.7) 0%, rgb(134, 191, 255,0.3) 130%), url('<?php echo $thumb;?>')">
            <h1 class="blogPostCard_title font-sans text-white font-bold text-start"><?php the_title(); ?></h1>
            <!-- Gettng custom taxonomies associate with teh post -->
            <?php
                $terms = get_the_terms($post_id, 'acquisition');

                if ($terms && !is_wp_error($terms)) {
                    $first_term = reset($terms); ?>
                    <span class="blogCard_taxonomy__item py-1 px-4 text-sm rounded-2xl absolute bottom-4 right-4 font-medium item-<?php echo $first_term->slug; ?>"><?php echo $first_term->name; ?></span>
                <?php
                }
            ?>
            <!-- Reading time -->
            <div class="blogPost_readingTime__wrapper">
                <?php 
                $readingTime = get_field('reading_time');
                ?>
                <div class="blogPost_readingTime text-white text-avenir absolute bottom-4 left-4">
                    <i class="fa-regular fa-lightbulb"></i>
                    <span><?php echo $readingTime; ?></span>
                </div>
            </div>
            <a href="#" type="button" class="view-post" data-postid="<?php the_ID(); ?>" data-slug="<?php echo get_post_field('post_name', $post_id); ?>"></a>
            </div>
        </div>
    </div>
<?php
$i++;
wp_reset_postdata();
}
?>

child query template:

<?php 

// Query to load ads on front-page inside while loop for posts
$adArgs = array(
    'post_type'      => 'ads', // Replace 'your_custom_post_type' with the actual name of your custom post type
    'posts_per_page' => 1,
    'orderby'        => 'rand', // Order by random
);

$adsQuery = new WP_Query($adArgs);

while($adsQuery->have_posts()){
    $adsQuery->the_post(); ?>

        <div class="col-span-1 my-auto">
        <?php

        if( have_rows('ads_group') ):

        while( have_rows('ads_group') ): the_row();

        $adsLogo = get_sub_field('logo');
        $adsLogoSize = 'full';
        $adsText = get_sub_field('ad_description');
        $adsButtonText = get_sub_field('button_text');
        $adsButtonLink = get_sub_field('button_link'); 
        $adsButtonBackgroundColor = get_sub_field('button_background_color');
        ?>
        
        <div class="ads_logo__wrapper flex justify-center my-2 max-height-[65px]">
            <?php
            if( $adsLogo ) {
                echo wp_get_attachment_image( $adsLogo, $adsLogoSize );
            }
            ?>
        </div>
        <div class="ads_text__wrapper flex justify-center font-medium text-base text-center font-avenir tetxt-base text-avenir my-2">
            <?php echo $adsText; ?>
        </div>
        <div class="ads_button__wrapper flex justify-center my-2">
            <a class="text-white font-avenir font-bold py-1.5 px-4 text-sm rounded-lg" target="_blank" href="<?php echo $adsButtonLink ?>" style="background-color:<?php echo $adsButtonBackgroundColor; ?>"><?php echo $adsButtonText; ?></a>
        </div>

        <?php 
        endwhile;
        endif;
        ?>
        
        </div>
    <?php
    }
    wp_reset_postdata();
    ?>

Picture for better understanding: https://prnt.sc/ioPNwLnR4be0

Highlighted in green is correct output from custom post type and child query, but then after it somehow main query is making same post card for a same post from custom post type.

0

There are 0 best solutions below