Support

Account

Home Forums Add-ons Repeater Field Post Object inside Repeater only displaying first row

Solved

Post Object inside Repeater only displaying first row

  • This is killing me. I have a post_object as the only field in a repeater. If I switch to any other type of field, all rows are correctly spit out – but only the first row is getting output if a post_object field. I can print_r the object and see all rows. Here’s the page in question

    <?php
    $args = array(‘post_type’ => ‘menu’, ‘posts_per_page’ => -1, ‘orderby’=>’menu_order’,’order’=>’ASC’);
    $menus = new WP_Query($args);

    if ($menus->have_posts() ):
    echo ‘<div id=”the_menus”>’;
    while ($menus->have_posts()): $menus->the_post();

    $menu_name = get_the_title();
    $menu_slug = $post->post_name;

    echo ‘<div class=”menu ‘.$menu_slug.'”>’;

    if ( have_rows(‘appetizer_items’) ):
    echo ‘<h3>Appetizers</h3>’;
    echo ‘<div class=”divider”>’.$divider_dark.'</div>’;

    while ( have_rows(‘appetizer_items’) ): the_row();
    $appetizer_item = get_sub_field(‘appetizer_item’);
    $post = $appetizer_item;

    setup_postdata( $post );
    $title = get_the_title();
    echo ‘<h4>’.$title.'</h4>’;
    wp_reset_postdata();
    endwhile;
    endif;
    echo ‘</div>’;
    endwhile;
    echo ‘</div>’;
    wp_reset_postdata();
    endif;
    ?>

  • Your code would be easier to read if you used code tags.

    Below is your code with code tags and comments explaining what the likely problem is.

    
    <?php 
      
      // there is a main query that happens before this code is run
      
      // this is a new custom query, not the main query
      $args = array('post_type' => 'menu', 'posts_per_page' => -1, 'orderby'=>'menu_order','order'=>'ASC');
      $menus = new WP_Query($args);
      
      if ($menus->have_posts() ):
        echo '<div id="the_menus">';
        while ($menus->have_posts()): $menus->the_post();
      
          $menu_name = get_the_title();
          $menu_slug = $post->post_name;
      
          echo '<div class="menu '.$menu_slug.'">';
      
          if ( have_rows('appetizer_items') ):
            
            // we need a way to restore the inner loop post
            // this cannot be done with wp_reset_postdata()
            // this will store the current $menus post
            // so we can get it back later
            $temp_post = $post;
            
            echo '<h3>Appetizers</h3>';
            echo '<div class="divider">'.$divider_dark.'</div>';
      
            while ( have_rows('appetizer_items') ): the_row();
              $appetizer_item = get_sub_field('appetizer_item');
              $post = $appetizer_item;
              
              setup_postdata( $post );
              $title = get_the_title();
              echo '<h4>'.$title.'</h4>';
              
              // this next line is resetting post data 
              // to the main query post
              // not to your custom query post
              // you need to remove this statement
              // wp_reset_postdata();
              
              // and we don't need to restore the
              // $menus post until after the end
              // of the have_rows() loop
              
            endwhile;
          endif;
          // end of have rows
          // reset the inner loop post
          setup_postdata( $temp_post );
          echo '</div>';
        endwhile;
        echo '</div>';
        
        // reseat the main query postdata
        wp_reset_postdata();
        
      endif;
      
    ?>
    
  • Yes, I apologize tremendously for butchering the initial post – embarrassing. Thanks for cleaning up, John. In the time between my asking and you answering, I switched to a relationship field + foreach to get things working. But I believe this is exactly correct. Temporarily storing the inner loop post did not occur to me. Marking as solved, thanks again.

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

The topic ‘Post Object inside Repeater only displaying first row’ is closed to new replies.