Home › Forums › Add-ons › Repeater Field › Query by an array of values from an ACF repeater Post Object sub field › Reply To: Query by an array of values from an ACF repeater Post Object sub field
Hi there,
About your first issue.
When you’re using $artists = $select['value'];
in your loop : you reset $artists
variable every time a loop is completed.
That’s why you store only the last value of your repeater field.
I assume you’re working on the same file template.
First, create an array of post IDs contained in your repeater field.
Then, use this array to build your custom WP_Query.
//Let's create an empty array
$artists = array();
//Loop over your repeater field
if(have_rows('release_artist')):
while(have_rows('release_artist')): the_row();
$related_artists = get_sub_field('artist');
//Working on post object
if( $related_artists && is_object($related_artists) && !is_wp_error($related_artists) ) {
$artists[] = $related_artists->ID;
}
//Working on an array
if( $related_artists && is_array($related_artists) ) {
foreach($related_artists as $post_object) {
if( is_object($post_object) && !is_wp_error($post_object) ) {
$artists[] = $post_object->ID;
}
}
}
endwhile;
//You should have an array containing all post IDs from release_artists repeater
//error_log(print_r($artists, true));
endif;
//Probably better to check if everything is valid in $artists array from there
//Then you could use your array of post ID to build your custom Query
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$artist_release_args = array(
'post_type' => 'releases',
'post_status' => 'publish',
'post__in' => $artists,
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => 6,
'paged' => $paged
);
$artist_release_query = new WP_Query($artist_release_args);
Don’t forget to check if your query has posts when you loop over it.
Add content-archive/content-none accordingly (or any template part needed).
Note that is_object()
and !is_wp_error()
checks are probably not strictly necessaries.
ACF normally returns an empty value if no post is selected in your sub field.
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.