Home › Forums › Add-ons › Repeater Field › Repeater field rows don't show up
Hey Elliot,
I’ve got a repeater field set up for a custom post type (‘gameresults’) and I just want to produce the repeater field values for a series of posts.
I’m attempting to copy the code and it’s not working. The code as given in the examples doesn’t show how the repeater field would be embedded in a loop to grab custom post types, but as far as I can tell, I should be able to just do a loop through my custom post type and insert the code. However, none of the three examples in the Repeater code samples work.
The single field in the ACF fieldset is ‘game_results_repeater.’
Its subfields are game_results_home_team, game_results_home_score, game_results_visitor_team, and game_results_visitor_score. However, I’m just trying to paste your example code first.
Here’s my code exactly:
<?php $posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'gameresults',
'orderby' => 'title',
'order' => 'ASC'
));
if($posts)
{
foreach($posts as $post)
{
?>
<h3 style="margin-top: 0px;">
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<?php if(get_field('game_results_repeater'))
{
echo '<ul>';
while(has_sub_field('game_results_repeater'))
{
echo '<li>sub_field_1 = ' . get_sub_field('game_results_home_team') . ', sub_field_2 = ' . get_sub_field('game_results_home_score') .', etc</li>';
}
echo '</ul>';
}
}
} ?>
I get the titles of my gameresults entries but no repeater field. Do I have to do a different kind of query in the main loop surrounding the repeater field to produce the repeater field subfields?
Thanks!
Hi @oxygensmith,
You’ll have to setup postdata for it to work without specifying the second post id parameter..
so put this before the if statement:
setup_postdata();
and then this right after the foreach loop
wp_reset_postdata();
Hey Jonathan, thanks. So, now my code looks like this:
<?php $posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'gameresults',
'orderby' => 'title',
'order' => 'ASC'
));
setup_postdata();
if($posts)
{
foreach($posts as $post)
{
?>
<h3 style="margin-top: 0px;">
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<?php if(get_field('game_results_repeater'))
{
echo '<ul>';
while(has_sub_field('game_results_repeater'))
{
echo '<li>sub_field_1 = ' . get_sub_field('game_results_home_team') . ', sub_field_2 = ' . get_sub_field('game_results_home_score') .', etc</li>';
}
echo '</ul>';
}
}
wp_reset_postdata();
} ?>
But when I load the page, I get this error:
Warning: Missing argument 1 for setup_postdata(), called in /home/lcbiskca/public_html/wp-content/themes/lcbi/sidebar-scores.php on line 20 and defined in /home/lcbiskca/public_html/wp-includes/query.php on line 3625
Any ideas? It seems like setup_postdata() needs to have an argument, so I inserted $posts but, still, nothing appears.
Thanks again for any help you can give.
Hi @oxygensmith
The setup_postdata();
function shoudl be passed the $post within the loop.
You can see an example of this here:
http://www.advancedcustomfields.com/resources/field-types/post-object/
Hope that helps
Like elliot says,
You should put the setup_postdata(); INSIDE the foreach loop. My bad wasnt thinking 🙂
The topic ‘Repeater field rows don't show up’ 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.