Support

Account

Home Forums ACF PRO get a repeater-field with an object-field in a repeater-field

Solved

get a repeater-field with an object-field in a repeater-field

  • Hello,

    I have a repeater field and would like to use an object field to read out a repeater field from another field group.

    I use the following code:

    
    <?php if ( have_rows( 'repeater_field' ) ) : ?>
    	<?php while ( have_rows( 'repeater_field' ) ) : the_row(); ?>
    		<?php $post_objects = get_sub_field( 'object_field' ); ?>
    		<?php if ( $post_objects ): ?>
    			<?php foreach ( $post_objects as $post ):  ?>
    				<?php setup_postdata( $post ); ?>
    
    <!-- This displays the contents of the repeater field from the other field group. --> 
    		<?php $post_object = get_field( 'object_field' ); ?>
    			<?php if ( $post_object ): ?>
    				<?php $post = $post_object; ?>
    				<?php setup_postdata( $post ); ?> 
    					<?php if ( have_rows( 'repeater_field' ) ) : ?>
    						<?php while ( have_rows( 'repeater_field' ) ) : the_row(); ?>
    							<?php the_sub_field( 'sub_field' ); ?>
    						<?php endwhile; ?>
    					<?php endif; ?>
    				<?php wp_reset_postdata(); ?>
    			<?php endif; ?>
    				  
    		<?php endforeach; ?>
    		<?php wp_reset_postdata(); ?>
    	<?php endif; ?>
    		
    <?php endwhile; ?>
    <?php endif; ?>
    

    The output is correct for the first element. The second, however, outputs the content of the first.
    What do I have to adapt to my code so that the correct content is output in each case?

  • Hello John,
    Thank you so much for your feedback.
    I understand the problem with the nested loops. But I don’t understand your examples behind the links.
    Maybe you can help me with a small example related to my code?
    Many thanks

  • You need to remove the nested setup_postdata() and wp_reset_postdata() and use some other method of referencing these nested posts. This includes using some other variable name for the post object besides $post.

    Here is your code with some modification

    
    <?php 
    if ( have_rows( 'repeater_field' ) ) :
      while ( have_rows( 'repeater_field' ) ) : the_row();
        $post_objects = get_sub_field( 'object_field' );
        if ( $post_objects ):
          foreach ( $post_objects as $post ): 
            setup_postdata( $post );
            $post_object = get_field( 'object_field' );
              if ( $post_object ):
                  if ( have_rows( 'repeater_field', $post_object->ID ) ) :
                    while ( have_rows( 'repeater_field', $post_object->ID ) ) : the_row();
                      the_sub_field( 'sub_field' );
                    endwhile;
                  endif;
              endif;
              
        endforeach;
        wp_reset_postdata();
      endif;
        
    endwhile;
    endif;
    ?>
    
  • Hello John,

    thanks for the code. I have implemented it and replaced the corresponding fields (repeater_field, object_field & sub_field) with my data.
    But now no more output is generated.

    Have I forgotten anything else?

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

The topic ‘get a repeater-field with an object-field in a repeater-field’ is closed to new replies.