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?
The problem is nested loops using post objects, setup_postdata() and wp_reset_postdata(). This question should be in the FAQ.
https://support.advancedcustomfields.com/forums/topic/exclude-current-post-in-foreach/
https://support.advancedcustomfields.com/forums/topic/problema-withe-repeater-item/
https://support.advancedcustomfields.com/forums/topic/hack-for-nested-queries/
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?
The topic ‘get a repeater-field with an object-field in a repeater-field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.