Support

Account

Home Forums Add-ons Repeater Field Repeater loop in a wp_query loop Reply To: Repeater loop in a wp_query loop

  • The problem is that the loop that starts with

    
    while ($subloop->have_posts()) : $subloop->the_post();
    

    is the second nested post loop. When you call

    
    $subloop->reset_postdata();
    

    This is resetting the post data to the post in “The Loop” and not the post data of your first nested loop. This is a common error.

    When you have

    
    The Loop (main WP query loop)
        => Nested loop
            => Nested loop
    

    you must loop through the posts in the second nested loop as well as any more deeply nested loop in a way other than by using the standard WP while (have_posts()) method.

    For example, instead of your code

    
      while ($subloop->have_posts()) : $subloop->the_post();
      if (have_rows('mega_menu')):
        while (have_rows('mega_menu')) : the_row();
          echo the_sub_field('menu_overskrift');
          while (have_rows('mastersidelinks')) : the_row();
            $subnavitemID = get_sub_field('masterside_sub_links', false, false);
            echo get_the_title($subnavitemID);
          endwhile;
        endwhile;
      else :
      endif;
      endwhile;
      $subloop->reset_postdata();
    

    you need to use something like this

    
      if (count($subloop->posts)) {
        // do not use $posts as a variable
        // because it will overwrite $post in the parent loop
        foreach ($subloop->posts as $nested) {
          $post_id = $nested->ID;
          while(have_rows('mastersidelinks', $post_id)) {
            the_row();
            $subnavitemID = get_sub_field('masterside_sub_links', false, false);
            echo get_the_title($subnavitemID);
          } // end while have rows
        } // end foreach
      } // end if count posts