Home › Forums › General Issues › Taxonomy field type: filter to only show parents
Is there an easy way to filter a taxonomy field type to only show the top level parent terms?
Currently, there is no filter for the wp_list_categories
args within the taxonomy field.
You can edit the core/fields/taxonomy field to add on in, however this may be removed after updating.
I’ll add this to the to-do and add it into the core.
Cheers
E
Good news. I have just added and pushed to github an update for the taxonomy field.
This update contains a new filter allwoing you to customize the args used in the wp_list_categories
function.
You can use it like so:
add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_args', 10, 2);
function my_taxonomy_args( $args, $field )
{
// do stuff to $args
return $args;
}
🙂 fantastic, will test this out in the next couple of days, thanks
Hey, just wanted to let you know that this works great – was able to add this line:
$args['depth'] = 1;
to the filter function and only show the top level taxonomy terms in the edit screen
Great. I got it working too.
Is there a way this can be applied only to 1 specific taxonomy field? I have a filter that’s useful for one taxonomy field but not something I’d want to apply to all my taxonomy fields.
I tried in my add_filter function using ‘acf/fields/taxonomy/wp_list_categories/name=XYZ’ but that didn’t work (the field just displayed all the categories)
Alternatively I just might not be understanding this correctly.
Thanks
Hi @charlie
Answer provided on http://support.advancedcustomfields.com/forums/topic/taxonomy-field-filter-to-only-show-parents/
Compare $field[‘name’]
I updated taxonomy.php from the github version after finding out that 4.2.2 doesn’t include the $args array & application of filters. Will this be released in the next update? I’m just curious to make sure that this still works after the next ( or future ) updates. Thanks!
@elliot Awesome. Thanks!!
Also, I have to say that I love this plugin, it makes WordPress SO. MUCH. BETTER.
Thanks again for all your work!
This solution doesn’t work if you choose select type, only work’s for checkboxes. How can use this for dropdown select ?
For Select or Multiselect dropdown use the below code to query only parents.
function hide_child_taxonomies( $args, $field ) {
if( 'YOUR_FIELD_NAME' === $field['_name'] ) {
$args['parent'] = 0;
}
return $args;
}
add_filter('acf/fields/taxonomy/query', 'hide_child_taxonomies', 10, 3);
The topic ‘Taxonomy field type: filter to only show parents’ is closed to new replies.
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.