1

I'm using ACF, and displaying the Brands CPT. Inside of brands, I will want to setup a separate loop for posts containing certain tags.

My loop works, meaning that I'm able to access the brands and their ACF fields, but when I try to implement the posts loop inside of the brands loop, no brand_partners( ACF subfield ) are found.

I saw other people struggling with this, so I searched and found this:

Loop within a loop?

I tried updating my code, but it results in a timeout:

    <?php
$args = array(

    'posts_per_page' => -1,
    'post_type'      => 'brands',
);

$brands_query = new WP_Query( $args );

if ( $brands_query->have_posts() ) {
    $loop_counter = 1;
    while ( $brands_query->have_posts() ) {

        $brands_query->the_post();
        $posttags = get_the_tags();
        ?>

<div class="container col-6 col-md-3 partner">
    <div class="popup popup--maxwidth" onclick="popItUp()">
        <img src="<?php echo get_the_post_thumbnail_url(); ?>"></img>
        <span class="p-2 popuptext">
            <span class="popup--close" onclick="popItUp()"><i class="fa fa-close" style="font-size:36px; color:white;"></i></span>
            <div class="partner-container">
                <div class="partner-flex-1">
                    <img class="mx-auto pb-4 pr-2" src="<?php echo get_the_post_thumbnail_url(); ?>"></img>
                </div>
                <div class="partner-flex-2">
                    <h4 class="tagline--subheadline pt-2"><?php the_title(); ?></h4>
                    <img class=" mx-auto" src="<?php echo get_theme_file_uri( '/images/dots.png' ); ?>;" width="45">
                    <h5><?php the_excerpt(); ?></h5>
                </div>
            </div>
            <h3 class="tagline--subheadline pt-2">Related posts</h3>
            <img class="mx-auto pb-3" src="<?php echo get_theme_file_uri( '/images/dots.png' ); ?>;" width="45">

            <div class="container mr-1">
                <div class="d-flex">

                    <!-- INNER POSTS QUERY -->
                    <?php
                    $inner_args = array(
                        'posts_per_page' => 3,
                    );

                    $inner_query = new WP_Query( $inner_args );

                    if ( $inner_query->have_posts() ) {
                        $loop_counter = 1;
                        global $post;
                        $backup = $post;
                        while ( $inner_query->have_posts() ) {
                            $inner_query->the_post();
                            $posttags = get_the_tags();
                            ?>

<div class="col-12 col-md-6 col-lg-4 pr-0 pl-0 mr-2 ml-2 mt-4 mb-4 posts posts--container flex-row post-<?php echo $loop_counter; ?>"
    style="background-image: url('<?php echo get_theme_file_uri( '/images/blogbg.jpg' ); ?>">
    <a href="<?php echo get_the_permalink(); ?>">
        <div class="posts popup--img" style="background-image:url('<?php echo get_the_post_thumbnail_url(); ?>')" alt=""></div>

        <h5 class="posts posts--tagname"><?php echo $tag->name . ' '; ?></h5>
        <h3 class="posts posts--title"><?php echo the_title(); ?></h3>
        <h5 class="posts posts--tagname"><?php echo get_the_date(); ?></h5>
        <h6 class="posts posts--desc p-4"><?php echo get_the_content(); ?></h6>
</div>
</a>


<?php
                        }
                        $post = $backup;
                    }

                    ?>
                    <!-- INNER POSTS QUERY END -->

                </div>
            </div>


            <h3 class="tagline--subheadline pt-2">Partners</h3>
            <img class="mx-auto pb-3" src="<?php echo get_theme_file_uri( '/images/dots.png' ); ?>;" width="45">
            <div class="container col-10 partner d-block d-md-flex justify-content-center flex-lg-wrap">
                <?php



                if ( have_rows( 'brand_partners' ) ) :
                    while ( have_rows( 'brand_partners' ) ) :
                        the_row();
                        $partner_image = get_sub_field( 'brand_partner_image' );
                        // Do something...
                        ?>
                <div class="popup--partnerimage  col-md-4" style="background-image: url(
                        <?php
                        echo $partner_image;
                        ?>
 );"></div>
                        <?php
                    endwhile;
                                else :
                                    echo 'No partners found';
                                endif;
                                ?>


            </div>
    </div>
    </span>
</div>

        <?php
    }
}?>

Any clues as to what I am doing wrong?

Thanks!

1 Answer 1

0

Try defining the post_type in your $inner_args array

$inner_args = array(
    'post_type' => 'post',
    'posts_per_page' => 3
    );
3
  • Does not matter unfortunately. It does not timeout anymore, but it only runs the origina successfully loop but not the inner loop. Meaning the posts are skipped they just don't appear.
    – Pbalazs89
    Apr 20, 2022 at 16:13
  • 1
    have you tried resetting the query after your second query using wp_reset_query() ? it sounds like the second inner query is working but the ACF have_rows( 'brand_partners' ) is failing after the second loop runs? Also for the sake of clarification: are you trying to query the "brands" post-type or the "post" post-type with the second inner query? Apr 20, 2022 at 16:33
  • The issue was indeed resetting the query to the original. putting $brands_query->reset_postdata(); after the inner loops endwhile; solved the issue. Thanks for the help!
    – Pbalazs89
    Apr 21, 2022 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.