Support

Account

Home Forums Add-ons Flexible Content Field using a layout twice Reply To: using a layout twice

  • Ah, I understand the reason for what you’re doing with the template pare. There is a way to do this in WP, but it’s complicated.

    The main issue with post loops nested multiple levels is that after the first nesting things go south if you try to use wp_reset_postdata().

    This explanation is for anyone that comes across this in the future as well because it is a very common misunderstanding about how wp_reset_postdata() or even wp_reset_query() works.

    Basically

    
    // lets say that this is the main WP loop
    while(have_posts()) {
      the_post(); // this is the post that wp_reset_post_data() will reset to
      // now you do another query and you have another loop
      if ($nested_query->have_posts()) {
        while($nested_query->have_posts()) {
          $nested_query->the_post();
          // then if you have a 3rd nesting, for example an ACF relationship field
          $related = get_field('relationship_field'));
          foreach ($related as $post) {
            setup_postdata($post);
            
            // do stuff
            
            // resetting post data here completely screws up the nested query loop
            // instead of resetting to the post of the nested loop
            // it resets to the post of the main loop
            wp_reset_postdata();
          }
        } // end while nested posts
        wp_reset_postdata(); // this works correctly
      } // end if nested posts
    } // end main while
    

    rather than using setup_postdata() and wp_reset_postdata() in the 3rd (and beyond) nesting you need to manage these another way and not mess with the global $post value. For example when you say

    I got the Id’s a different way (got the whole object and then used $object->ID)