Support

Account

Home Forums Front-end Issues ACF Content not showing with multiple query_post

Solved

ACF Content not showing with multiple query_post

  • Hi all,

    I have a site with a page that shows posts from four different categories, along with some ACF content for each lot of posts. The code I am using is:

    // Display ACF content for category a
    <?php the_field('category_title_a'); ?
    <?php the_field('category_content_a'); ?>
    
    // Display posts from category a
    <?php $my_query = new WP_Query( 'category_name=cat-name' );
    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    //extra stuff here
    <?php endwhile; ?>

    THE PROBLEM I am having is that the ACF content shows up fine for each lot of posts if I use standard loops with no queries, but once I add a query for each loop to only select certain categories the ACF content AFTER the first loop doesn’t show.

    TO CLARIFY: When using multiple query_posts on a page the ACF content BEFORE first query-posts loop will show but the ACF content AFTER the first query_posts will not show.

    I’m no a super tight deadline and I can’t seem to find a solution.

    Please help!

    Thank you in advance

  • SOLUTION FOUND!

    I used this after each while:

    <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

    So the end product looks like this:

    // Display ACF content for category a
    <?php the_field('category_title_a'); ?
    <?php the_field('category_content_a'); ?>
    
    // Display posts from category a
    <?php $my_query = new WP_Query( 'category_name=cat-name' );
    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    //extra stuff here
    <?php endwhile; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
  • Hi brendan,
    I’m having similar problem, I cann’t show ACF fields content, only WordPress “content” textarea value is shown, could you help me?
    My code:

    
        $args       = array( 'post_type' => 'page', 'name' => 'movies', 'post_status' => 'publish' );
        $wp_query   = new WP_Query( $args );
        $output     = "";
    
        while ( $wp_query->have_posts() ) {
            $wp_query->the_post();
            $output .= apply_filters('the_content', get_the_content());    
        }
        
        wp_reset_query();
    
        return $output;

    Thank you

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.