
I have worked with ACF Pro a lot and love the plugin. Recently I ran into an issue that I cannot figure out. I’m making a list of committees (custom post type) and committee members (the repeater field). The sub field is member.
THE ISSUE: All posts list perfectly except the first one. It does not matter if I reverse the order, it is always the first post. No members are listed, only the committee name. Here’s my code (simplified and very standard):
$args = array(
'post_type' => 'committee',
'posts_per_page' => -1,
),
'orderby' => array('title' => 'ASC'),
);
$the_query = new WP_Query($args);
$committees = '';
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$committees .= '<p><strong>'.get_the_title().'</strong>';
if( have_rows('committee_members') ):
while( have_rows('committee_members') ): the_row();
$committees .= '<br>* '.get_sub_field('member');
endwhile;
endif;
$committees .= '</p>';
endwhile;
endif;
echo $committees;
Interestingly, the if (have_rows('committee_members'))
is true for the first post, but while( have_rows...)
prints nothing, like it is skipped. Check out the listing at https://acccind.org/2019/association-committees/.
The database records look right. The data is there. I have other very similar code that work just fine. Go figure.
I have learned that if I use PHP foreach
code, I can get the sub field values. This code snippet works:
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$committees .= '<p><strong>'.get_the_title().'</strong>';
$members = get_field('committee_members' );
foreach ($members as $member) {
$committees .= '<br>'.$member['member'];
}
$committees .= '</p>';
endwhile;
endif;
The previous code using while( have_rows() )
does not, again only for the first post.