Home › Forums › General Issues › Multi Select – Get all choices for Query Args
I want to grab all even categories chosen by user in an ACF Gutenberg Block where they can multi select categories. I now only grab the first slug in the acf field array which is one of the event_categories
. I need to grab all to filter the posts loaded.
Here is my code now
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_category_field = get_field('events_category');
print_r($event_category_field);
// $event_object = get_field_object('events_category');
// print_r($event_object);
// $event_cat_name = $event_object['value'];
$event_cat_slug = $event_category_field[0]->slug;
// print_r($event_cat_slug);
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
'event_category' => $event_cat_slug
);
As you can see $event_cat_slug = $event_category_field[0]->slug;
only grabs the first slug (event category lower case) for the first key. How do I grab slug for all keys and use that for event_category
that only takes a slug as a string?
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_object = get_field_object('events_category');
// print_r($event_object);
$event_cat_name = $event_object['value'];
// print_r($event_cat_name);
$cat_names = array_column($event_cat_name, 'slug');
print_r($cat_names);
// $event_cat_slug = $event_cat_name[0]->slug;
// print_r($event_cat_slug);
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
'tax_query' => array(
array(
'taxonomy' => 'event_category',
'terms' => $cat_names
)
)
);
Still does not filter though $cat_names
does print cats selected in multi select like when I choose onsite only: Array ( [0] => onsite )
terms should accept an array I understand but JS states
Error: Syntax error, unrecognized expression: Array
(
[0] => onsite
)
<h2>Event 4 Virtual</h2>
<h2>Event 3</h2>
<h2>Event Two</h2>
Reading https://support.advancedcustomfields.com/forums/topic/filtering-with-posts-that-are-using-multiple-select/ it seems I may need meta_query
When you allow multiple values the values saved is no longer a single text value. Instead it is stored in the DB as a serialized array. You need to alter the meta query
but tried a few things and not yet sure how..
No, you would use a tax_query. The tax query you tried requires an array of term ID values by default. To use slugs you would need to include the “field” argument in the tax_query. https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
I was yesterday recommend to use something similar:
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => block_value('event-category')->name,
],
So to work with ACF fields I would then use
<?php
/**
* Events CPT Block
*
**/
// Create id attribute allowing for custom "anchor" value.
$id = 'pm-' . $block['id'];
if( !empty($block['anchor']) ) {
$id = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'block-pm';
if( !empty($block['className']) ) {
$className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
$className .= ' align' . $block['align'];
}
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_object = get_field_object('events_category');
// print_r($event_object);
$event_cat_name = $event_object['value'];
$event_cat_slug = $event_cat_name[0]->slug;
// print_r($event_cat_slug);
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
// 'event_category' => $event_cat_slug
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => $event_cat_slug, //
],
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
not sure yet how to get term ids loaded from the events_category
field. So far I have only been able to get arrays:
print_r($event_cat_name);
Array ( [0] => WP_Term Object ( [term_id] => 179 [name] => Virtual [slug] => virtual [term_group] => 0 [term_taxonomy_id] => 179 [taxonomy] => event_category [description] => [parent] => 0 [count] => 3 [filter] => raw [meta] => Array ( ) ) [1] => WP_Term Object ( [term_id] => 180 [name] => Local [slug] => local [term_group] => 0 [term_taxonomy_id] => 180 [taxonomy] => event_category [description] => [parent] => 0 [count] => 1 [filter] => raw [meta] => Array ( ) ) )
or the first slug of slugs given by chosen event categories:
print_r($event_cat_slug);
virtual
I can switch from term object to term id. But still, how to load the term ids chosen in a variable I can add to terms in the tax_query
? Any suggestions?
When I switch to term id for the field and use
$event_cats_ids = get_field('events_category');
var_dump($event_cats_ids);
I get this array
array(2) { [0]=> int(179) [1]=> int(180) }
So the situation is similar. I get an array. Using that in
'terms' => $event_cats_ids,
does not work. When I choose one local event I get the last virtual event. So I think this does not work.I need array(id, id),
based on variable input somehow.
Reading https://www.advancedcustomfields.com/resources/taxonomy/#template-usage via one of the threads you helped with @hube2 I realized I can of course loop through the array:
<?php
$terms = get_field('events_category');
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo esc_html( $term->slug ); ?>
<?php endforeach; ?>
<?php endif; ?>
and then I do get local virtual
.Just not with commas to use in wp_query
terms
yet. Will need some formatting to be used as variable in
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => $event_cat_slugs,
That and I wonder if there is a quicker way to get these for my variable. A foreach loop seems a bit much..
Do now have this
// 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;
echo $term_slugs;
which almost works for loading this is terms => array(terms)
:
$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 I need a comma after each term except for the last one
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.
Saw something funny happening to the quotes so used decimal code for quotes:
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$terms = get_field('events_category');
// print_r($terms);
// $term_slugs = $terms()->slug;
$term_slugs ='';
foreach ( $terms as $term ):
$term_slugs .= "'" . esc_html( $term->slug ) . "'" . ',';
endforeach;
$term_slugs = substr($term_slugs, 0, -1); //Removes very last comma.
// print_r($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_values($term_slugs),
],
);
still no filtering on event category..
Does something like the below work:
$event_cat_ids = array( 37, 47 );
$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(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; wp_reset_postdata();
endif;
Replace the values in the array to some cat IDs just to test the code.
If that works, you then need to adjust to suit your needs i.e. use your field value.
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
@jarvis Thanks for that. Did add the option to choose number of posts as well so adjusted the code and am now using:
/ Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$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' => $number_of_events,
'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(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata();
endif;
?>
I see two times the one and only post for event category local.
This when I choose 1 event and category local. So somehow things repeat themselves. Two, I see a warning
Warning: foreach() argument must be of type array|object, null given in /Users/jasper/code/site/wp-includes/class-wp-list-util.php on line 164
and when I print print_r($event_cat_ids);
I see
Array ( [0] => 180 )
Event Two Local
Array ( [0] => 180 )
Event Two Local
so closer, but not quite.
$event_categories = get_field('event_categories');
var_dump($event_categories);
gives us
array(2) { [0]=> object(WP_Term)#4557 (11) { [“term_id”]=> int(179) [“name”]=> string(7) “Virtual” [“slug”]=> string(7) “virtual” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(179) [“taxonomy”]=> string(14) “event_category” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(3) [“filter”]=> string(3) “raw” [“meta”]=> array(0) { } } [1]=> object(WP_Term)#4445 (11) { [“term_id”]=> int(180) [“name”]=> string(5) “Local” [“slug”]=> string(5) “local” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(180) [“taxonomy”]=> string(14) “event_category” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(1) [“filter”]=> string(3) “raw” [“meta”]=> array(0) { } } }
and
array(1) { [0]=> object(WP_Term)#4581 (11) { [“term_id”]=> int(180) [“name”]=> string(5) “Local” [“slug”]=> string(5) “local” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(180) [“taxonomy”]=> string(14) “event_category” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(1) [“filter”]=> string(3) “raw” [“meta”]=> array(0) { } } }
with two categories chosen and three for number of events.
So as these are two arrays with arrays we get
NULL
Warning: foreach() argument must be of type array|object, null given in /Users/jasper/code/site/wp-includes/class-wp-list-util.php on line 164
Hmm
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$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' => $number_of_events,
'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(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata();
endif;
?>
does work now. Virtual 3 shows me `Event 4 Virtual Event 3 Virtual Event One Virtual
` Only display of block on page in editor is hidden all the time on save. Perhaps related to
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at toggle.js?ver=5.8.1:7
Hmm
<?php
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$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' => $number_of_events,
'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(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata();
endif;
?>?>
does work now. Virtual 3 shows me
Event 4 Virtual Event 3 Virtual Event One Virtual
Only display of block on page in editor is hidden all the time on save. Perhaps related to
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at toggle.js?ver=5.8.1:7
Not sure yet.
Those warnings are for a foreach loop, my code didn’t include a foreach loop, so something else is causing the issue.
Can you switch to the default twenty-twentyone theme and add the code (probably index.php)
See if that works?
The code I supplied was tried and tested. So something else is causing your issue, especially a toggle.js issue as again, this isn’t linked to my code
I replaced the local database with the latest one to be sure I had no leftover blocks experimenting with all. Still I see
TypeError: null is not an object (evaluating 'e.addEventListener')
Global Code — toggle.js:7
Selected Element
<div class="block-editor-block-list__layout is-root-container" data-is-drop-zone="true" data-select2-id="3">…</div>
so perhaps it is caused by another block. Annoying though as it kills display of my new block in the editor. Seems to be related to a WordPress default block as the class is called block-editor-block-list__layout
. Will try to see what it is.
Checking console in Firefox Developer I now see
Uncaught TypeError: can't access property "addEventListener", e is null
<anonymous> https://site.test/wp-content/themes/theme/blocks/pricing-toggle/toggle.js?ver=5.8.1:7
So will see if that block error is causing my block not to display well in the editor.
You must be logged in to reply to this topic.
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.