Support

Account

Home Forums Front-end Issues Repeater only showing one row

Helping

Repeater only showing one row

  • I have a relationship field that passes a post to a flexible content field. Now I have a block that’s a repeater and when run will only ever return one row even when I add more. Where am I going wrong?

    My Code: –

    
    <?php
    
    global $post; // Querying outside the loop
    $cta_block_item = get_sub_field( 'relationship_field', $post->ID );
    
    if ( $cta_block_item ):
    
    foreach ( $posts as $post ):
    setup_postdata ( $post );
    
    if (have_rows('page_builder')): // Get the rows of the Flexible content field of my target
    		while (have_rows('page_builder', $post->ID)):
    				the_row();
    
    			if (get_row_layout() == 'cta_grid') {
    
    			 $cta_grid_select = get_sub_field('cta_grid_select'); // get the custom field I want to get
    
    				if ( have_rows( 'row') ) :
    
    					while ( have_rows( 'row', $post->ID ) ) : the_row();
    
    							if ( $cta_grid_select ):
    							foreach ( $cta_grid_select as $post ):
    
    							setup_postdata ( $post );
    
    							// Expected: I should see all rows of my repeaters content
    
    							// Current Result: Returns only one row from the repeater
    
    							endforeach;
    							wp_reset_postdata();
    							endif;
    
    					endwhile;
    
    				else :
    				// no rows found
    				endif;
    
    			}
    
    		endwhile;
    
    else:
    		// no layouts found
    endif;
    
    endforeach;
    wp_reset_postdata();
    endif;
    ?>
    
  • I think that you’re problem is with multiple nested queries.

    For example:

    
    Main WP Query is run
    ==> A secondary query is run
    ====> A third query is run or you get a post from an ACF field
    

    In these cases you cannot use the global $post or the functions setup_postdata ( $post ); and wp_reset_postdata();

    The reason for this is the wp_reset_postdata(); always resets the post to the main query and not to the secondary post query. When dealing with post queries nested 3 or more deep you must use some other method of interacting with the post.

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

You must be logged in to reply to this topic.