Home › Forums › Add-ons › Repeater Field › Trying to retrieve page objects in repeater
Hi there,
Yes, it’s me again.
Not sure if this post belongs in the Repeater Field forum, or in the Options Page forum, but since it’s more a coding thing I’m putting it here. Feel free to move if necessary.
So, what I’m trying to do here is to retrieve each post object (in this case, pages, including title and content) selected in a repeater field. Below the error code I’m getting is the code I’m trying to use. I tried to do a mix of the repeater field code and the code for the page object, but something’s not right (my row is called ‘home’, and the sub-field is ‘choose_pages’). I get the following error:
Catchable fatal error
: Object of class WP_Post could not be converted to string in /path/wp-content/plugins/advanced-custom-fields/core/api.php on line 690
<?php if( have_rows('home', 'option') ):
while( have_rows('home', 'option') ): the_row();
$post_objects = the_sub_field('choose_pages');
if( $post_objects ):
foreach( $post_objects as $post_object): ?>
<article class="px-content" id="chapter<?php $post_object->ID ?>">
<div class="px-inner">
<div class="container-fluid">
<div class="row">
<div class="headline">
<h1><?php echo get_the_title($post_object->ID); ?></h1>
<p>content</p>
</div>
</div>
</div>
</div>
</article>
<?php endforeach;
endif;
endwhile;
endif; ?>
I was just about to set this up to test because I didn’t see anything wrong with it when I noticed the problem when I looked back at your code to get the name of the sub field I needed to set up.
This $post_objects = the_sub_field('choose_pages');
Should be this $post_objects = get_sub_field('choose_pages');
I must have looked at the code 5 time trying to figure out what ACF was generating at error without seeing it.
Ahhhhhh!!! Thank you!
So, now I don’t get an error, but I’m also not getting any bits of the object. I thought I was grabbing the page ID and title with that code.
Also, it’s creating an endless loop of these “articles” instead of just grabbing the ones selected in the Repeater Field (currently there are only 5 selected)
Ahhhh…I seem to have gotten it to work now. Your help was invaluable. Thanks you!
if( have_rows('home', 'option') ):
while ( have_rows('home', 'option') ) : the_row();
$post_object = get_sub_field('choose_pages');
if( $post_object ):
$post = $post_object;
setup_postdata( $post ); ?>
<article class="px-content" id="chapter<?php echo the_ID(); ?>">
<div class="px-inner">
<div class="container-fluid">
<div class="row">
<div class="headline">
<h1><?php echo get_the_title($post->ID); ?></h1>
<p>content</p>
</div>
</div>
</div>
</div>
</article>
<?php wp_reset_postdata();
endif;
endwhile;
endif;
Sorry I did not get back to you last night, glad to hear you got it working. Let me know if you’re still having problems.
~JH
The topic ‘Trying to retrieve page objects in repeater’ 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.