Home › Forums › General Issues › Multi Select – Get all choices for Query Args › Reply To: Multi Select – Get all choices for Query Args
Hi @rhand
If your ACF field Event Categories is a taxonomy and its set to return the Term Object (and you have Load and Save terms selected), replace:
$event_cat_ids = array( 37, 47 );
With:
$event_categories = get_field('event_categories');
$event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
So now looks like:
<?php
$event_categories = get_field('event_categories');
$event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
#$event_cat_ids = array( 1, 31 );
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1, // show all posts.
'order' => 'DESC',
);
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'event_category',
'field' => 'term_id',
'terms' => $event_cat_ids
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata();
endif;
?>
I’ve tested this code and can confirm it works, returning the posts of the selected categories in your multi select
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.