Home › Forums › General Issues › Multi Select – Get all choices for Query Args › Reply To: Multi Select – Get all choices for Query Args
I think this is closer now:
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$terms = get_field('events_category');
$term_slugs ='';
foreach ( $terms as $term ):
$term_slugs .= esc_html( $term->slug ) . ',';
endforeach;
$term_slugs = substr($term_slugs, 0, -1); //Removes very last comma.
// echo $term_slugs;
// local,virtual,
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
// 'event_category' => $term_slugs
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => array($term_slugs),
],
);
but if I added term names I need to quote them as well still. So testing
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$terms = get_field('events_category');
$term_slugs ='';
foreach ( $terms as $term ):
$term_slugs .= "'" . esc_html( $term->slug ) . "'" . ',';
endforeach;
$term_slugs = substr($term_slugs, 0, -1); //Removes very last comma.
echo $term_slugs;
// 'local','virtual'
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
// 'event_category' => $term_slugs
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => array($term_slugs),
],
);
Seems though when I use local as category which has only one post and select 4 as the number of posts I still see all four.
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.