Home › Forums › General Issues › taxonomy field – filter to only show parents
I’ve successfully implemented the filter to only show parents – as per http://support.advancedcustomfields.com/forums/topic/taxonomy-field-type-filter-to-only-show-parents/
It works. But I was wondering is there a way I can apply this filter to one specific taxonomy field?
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).
My code is below:`
add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_args', 10, 2);
// filter the taxonomy shown in the admin area to children of a specific parent
function my_taxonomy_args( $args, $field )
{
$args['child_of'] = 38;
return $args;
}
`
Also you may wish to update your web page for this field (http://www.advancedcustomfields.com/add-ons/taxonomy-field/).
It states this field is only supported for ACF version 3 (which I’m assuming is not correct)?
Hi @charlie
Are you sure you are using the taxonomy add-on? Or are you using the taxonomy field which is part of ACF v4?
To run the filter on your specific field, you will need to look at the $field parameter of the filer like so:
if( $field['name'] != 'my_specific_name' )
{
return $args;
}
thanks @elliot. the code above makes sense. I must say I’m confused about the taxonomy field. The documentation page (http://www.advancedcustomfields.com/resources/) doesn’t say anything about a built in taxonomy field AFAIK.
I haven’t installed the add-on so I presume I’m using the built in one. Are they more or less the same?
Hi @charlie
They are both very different. The core taxonomy field is much more stable, so continue to use it.
Thanks
E
sure. It might be worth adding it to the field types list (http://www.advancedcustomfields.com/resources/#field-types)?
Thanks @elliot
The field[‘name’] didn’t work for me. I ended up with
[name] => fields[field_5266beccd0583][]
which wasn’t exactly what I was looking for.
I ended up doing a string position find (strpos) on the $field[‘id’] for the name and I came up with the answer.
I hope this helps!
hi @elliot
I’ve just recently implemented your suggestion. Yet, as @socki03 has correctly indicated, field['name']
does not yield the correct result. If I recall correctly it actually returns the whole object array.
His suggestion of using strpos didn’t seem quite right to me (no disrespect) so I’ve done this.
if ($field['key'] == 'field_524561acf76e5')
This seems kosher (I hope) but hard-coding the field key seems to not be ideal. If I had to recreate that field (on another server for example) I’d have to update the key I imagine.
Is there a way a specific field can be targeted using the field name?
Hi guys.
You are quite right. During some of the actions, the $field[‘name’] will be changed to the input name.
I will include in the next update a ‘backup’ of the original name at $field[‘field_name’]
How does this sound? Or can you think of a nicer key for this data besides ‘field_name’? Perhaps ‘_name’?
Thanks
E
great. That key seems fine to me @elliot
It would be nice if we could specify a specific field in the filter function the way you can for other fields – e.g acf/fields/taxonomy/wp_list_categories/name=XYZ but not sure if that’s possible?
thanks
Hi
i just changed field[‘name’] to field[‘_name’] and it works for me.
@pooya is correct, field['_name']
works for checklists (not select or multiselect) taxonomy fields.
function taxonomy_depth( $args, $field ) {
if( 'TAXONOMY_NAME' === $field['_name'] ) {
$args['depth'] = 1;
}
return $args;
}
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 – 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.