Support

Account

Home Forums Add-ons Flexible Content Field Content after custom loop inside flexible content

Solved

Content after custom loop inside flexible content

  • Hi all!
    I’m facing some issues to display content inside a custom post type after a custom WP loop (WP_Query) inside ACF flexible content.

    To display content before the WP_Query works fine. But not after.

    wp_reset_postdata() is implemented after the end of the custom loop. But without any luck.

    The custom post type handle the index content for the page.
    And ACF Flexible Content handles the content inside each page. This to be able to have multiple “sections” on one page.

    Se the code below.
    Shorted down and cleaned from all the HTML. Since it’s not essential in the problem.

    
    <?php
    get_header();
    $args = array(
      'post_type'       => 'sm_indexsections',
      'posts_per_page'  => -1
    );
    $query = new WP_Query($args);
    
    // The custom post type loop
    if($query->have_posts()):
      while ($query->have_posts()):
        $query->the_post();
    
          // ACF Flexible content loop
          if(have_rows('acfIndCont')) {
            while(have_rows('acfIndCont')) {
              the_row();
              switch(get_row_layout()) {
    
                // Show blog-posts where a certain criteria is met
                case 'acfIndSecBlogPosts':
                    $postsArgs = array(
                      'posts_per_page'  => 5,
                      'post_type'       => 'post',
                      'meta_key'        => 'acfKnowHubPostCaseShow',
                      'meta_value'      => true
                    );
    
                    $postsQuery = new WP_Query($postsArgs);
    
                    if($postsQuery->have_posts()) {
                      while($postsQuery->have_posts()) {
                        $postsQuery->the_post();
                        // Display all blog-posts where this criteria is met
                      }
                    }
    
                  // Tried reset_query aswell but wit no success.
                  // wp_reset_query();
                  wp_reset_postdata();
                  break;
    
                // To put this (or other flexible content) before the custom loop works. But not after.
                // there is no loop inside this case. 
                case 'acfIndSecBtn':
                  // Other content to display
                  break;
              }
            }
          }
      endwhile;
    endif;
    wp_reset_postdata();
    get_footer();
    ?>
    

    Can anybody see what’s wrong with this? With my knowledge this should work. But it doesn’t.

    Thanks!

  • Try moving your wp_reset_postdata() calls inside your if($query->have_posts()) and if($postsQuery->have_posts()) statements.

    https://developer.wordpress.org/reference/functions/wp_reset_postdata/#comment-2325

  • Thanks for your answer.

    Hmm. Nope. didn’t work.
    Forgot to mention that i did try to move the call inside if($postsQuery->have_posts())earlier.

  • The problem here is that you have multiple nested queries. This comes up often. When you have multiple nested queries you cannot loop over the posts in the 3rd, 4th, etc, queries using the standard WP loop

    
    while ($query->have_post()) {
      .... etc
    

    The reason for this is that wp_reset_postdata() always resets the query and the post data to the main WP query and not the previous WP query.

    see my notes and changes

    
    <?php
    
    get_header();
    
    // main WP query happens before here
    
    // this is the 1st nested query
    $args = array(
      'post_type'       => 'sm_indexsections',
      'posts_per_page'  => -1
    );
    $query = new WP_Query($args);
    
    // The custom post type loop
    if($query->have_posts()):
      while ($query->have_posts()):
        $query->the_post();
    
          // ACF Flexible content loop
          if(have_rows('acfIndCont')) {
            while(have_rows('acfIndCont')) {
              the_row();
              switch(get_row_layout()) {
    
                // Show blog-posts where a certain criteria is met
                case 'acfIndSecBlogPosts':
                  
                  // This is the second nested query
                  
                    $postsArgs = array(
                      'posts_per_page'  => 5,
                      'post_type'       => 'post',
                      'meta_key'        => 'acfKnowHubPostCaseShow',
                      'meta_value'      => true
                    );
    
                    $postsQuery = new WP_Query($postsArgs);
                    
                    // you must access the posts in this query useing other methods
                    
                    
                    /* NOT THIS
                    if($postsQuery->have_posts()) {
                      while($postsQuery->have_posts()) {
                        $postsQuery->the_post();
                        // Display all blog-posts where this criteria is met
                      }
                    }
                    END NOT THIS */
                    
                    // USE SOMETHING LIKE THIS
                    // also do not use the global $post varaible
                    if (count($postsQuery->posts)) {
                      foreach ($postsQuery->posts as $nested_post) {
                        // and exampele
                        echo get_the_title($nested_post->ID);
                      }
                    }
    
                  // DO NOT USE EITHER OF THESE FUNCTIONS
                  // IN 2ND AND AFTER NESTED QUERIES
                  // wp_reset_query();
                  //wp_reset_postdata();
                  break;
    
                // To put this (or other flexible content) before the custom loop works. 
                //But not after.
                // there is no loop inside this case. 
                case 'acfIndSecBtn':
                  // Other content to display
                  break;
              }
            }
          }
      endwhile;
    endif;
    wp_reset_postdata();
    get_footer();
    ?>
    
  • You sir are a genius! It works 🙂
    Thank you so much!

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

The topic ‘Content after custom loop inside flexible content’ is closed to new replies.