Is it possible to loop through wordpress posts by a repeater fields content?
I have a event where you can add events through a repeater field (ACF) and I would like to show all dates for each event.
How do I do that?
Yes, you can use a meta_query to identify posts with that key and then loop through the repeater fields. It should look something like below.
$args = array(
'meta_query' => array(
array(
'key' => 'event_date', //Or whatever your
'compare' => 'EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ){
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
the_sub_field('sub_field_name');
endwhile;
else {
// no rows found
}
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();