Support

Account

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

  • 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();
    ?>