Hi I am attempting to get the post object from a repeater field but I can’t quite get it working.
The code I currently have displays the current page title instead of the post selected with the post object in my repeater field.
It displays the correct number of rows set in the repeater field just not the post object.
I am using $user_id = get_current_user_id(); since the repeater field is set to display when editing a users profile.
<ul> <!-- Start Repeater --> <?php $user_id = get_current_user_id(); if( have_rows('repeater', 'user_'.$user_id)): // check for repeater fields ?> <?php while ( have_rows('repeater', 'user_'.$user_id)) : the_row(); // loop through the repeater fields ?> <?php // set up post object $post_object = get_sub_field('sub-repeater-field', 'user_'.$user_id); if( $post_object ) : $post = $post_object; setup_postdata($post); ?> <li> <?php the_title(); ?></li> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?> <?php endwhile; ?> <!-- End Repeater --> <?php endif; ?> </ul>
Any assistance would be greatly appreciated.
get sub field doesn’t require giving the post id, change to this
but I’m not sure that will fix the problem. Make that change and see what happens.
If that does not fix it. is the post object field set to allow multiple selections or a single selection?
You must be logged in to reply to this topic.
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!
© 2022 Advanced Custom Fields.