Support

Account

Home Forums Add-ons Repeater Field Repeater loop in a wp_query loop

Solved

Repeater loop in a wp_query loop

  • Hi. I’m having trouble to making a repeater in a repeater loop, to work.

    I think the problem is around the if loop (if ( get_sub_field(‘mega_menu_active’)).
    Any ideas?

    Thank you
    Kris

    <?php
    $thistitle = get_the_title();
    $args = array('post_type' => 'navigation', 'pagename' => 'Top navigation');
    $loop = new WP_Query($args);
    
    while ($loop->have_posts()) : $loop->the_post();
    
        if (have_rows('navigation')):
            while (have_rows('navigation')) : the_row();
    
                //from this if
                if (get_sub_field('mega_menu_active'))
                { 
                    //mega_menu_active is a true/false
                    $navitemID = get_sub_field('masterside', false, false);
    
                    $subargs = array('post_type' => 'navigation', 'page_id' => $navitemID);
                    $subloop = new WP_Query($subargs);
    
                    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();
                } else
                {
                    include(locate_template('templates/navigation/navigation-top.php'));
                }
    
            endwhile;
        else :
        endif;
        wp_reset_postdata();
    endwhile;
    ?>
  • 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
    
  • @john thank you so much for all your effort. I tried your solution, and some others – and finally it worked. It seems that the main error was that reset_postdata was used on the wrong loop. So here is the code that is working

    <?php
    	$thistitle = get_the_title();
    	$args = array( 'post_type' => 'navigation', 'pagename' => 'Top navigation' );
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    	if ( have_rows('navigation') ):
    	    while ( have_rows('navigation') ) : the_row();
    			if ( get_sub_field('mega_menu_active')) {
    
    				$navitemID = get_sub_field('masterside', false, false);
    
    					$subargs = array( 'post_type' => 'navigation', 'page_id' => $navitemID);
    					$subloop = new WP_Query( $subargs );
    
    					while ( $subloop->have_posts()) : $subloop->the_post();
    				        if( have_rows('mega_menu')):
    				            while (have_rows('mega_menu')) : the_row();
    				              echo get_sub_field('menu_overskrift');
    				                while (have_rows('mastersidelinks')) : the_row();
    				                  $subnavitemID = get_sub_field('masterside_sub_links', false, false);
    				                  echo get_the_title($subnavitemID);													echo get_the_permalink($subnavitemID);
    				                endwhile;
    												
    			              endwhile;
    				        endif;
    							$loop->reset_postdata();
    					endwhile;
    				} else {
    					include(locate_template('templates/navigation/navigation-top.php'));
    				}
    	    	endwhile; else : endif; endwhile;
    	?>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.