I’m hoping this is a simple fix.
Much like you can select a taxonomy type on a field of type TAXONOMY (when creating a custom field), I need to be able to select ONLY categories (not tags or other custom taxonomies) on a RELATIONSHIP field. So when an editor chooses “Select taxonomy,” only the categories appear in the dropdown.
I’ve exhausted my search on ACF filters. Please advise.
more context:
With the relationship field, I can only choose the categories one by one, rather than just choose “categories,” as you can with a Taxonomy custom field. So, as categories change, I’d have to adjust each relationship field, and there are MANY.
Taxonomy custom field = filter by taxonomy TYPE
Relationship custom field = filter by taxonomy TERM
Need the relationship custom field to filter by ALL categories (and ONLY categories, not tags) without selecting each one manually.
Hi rhcarlosweb, unfortunately there wasn’t a solution to this. I did get an official response from ACF confirming this, but I can’t seem to find it.
Since the client had a limited number of categories, I just selected each one of them in the “Filter by Taxonomy” field. Definitely not ideal, but it works for now.
I’ve been thinking about this and there could be a solution to ensure that all of the terms in a taxonomy are always included/selected for the filter. This could be done with an acf/load_field filter. Export the taxonomy field to php to get an idea of the settings for the taxonomy selections. In the filter get all of the terms in the taxonomy and populate the acf field setting with the correct values. This way, if a term is ever added to the category it will be automatically added to the list.
Something like this :
function load_recipe_category_filter( $field ) {
$terms = get_terms( array(
'taxonomy' => 'recipe_category',
'hide_empty' => false
) );
if ( !empty($terms) ) {
foreach( $terms as $term ) {
$field['taxonomy'][] = 'recipe_category:'.$term->slug;
}
}
return $field;
}
add_filter('acf/load_field/key=field_5899e67cc9f5fff', 'load_recipe_category_filter');
So when an editor chooses “Select taxonomy,” only the categories appear in the dropdown. I’ve exhausted my search on ACF filters.
This works (for me)
add_filter('acf/load_field/key={FIELD_KEY}', 'load_just_categories_filter', 12, 1);
function load_just_categories_filter( $field ) {
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false
) );
if ( !empty($terms) ) {
foreach( $terms as $term ) {
$field['taxonomy'][] = 'category:'.$term->slug;
}
}
return $field;
}
Thank you mate. It works. If somebody has “Fatal error: [] operator not supported for strings” just add $field[‘taxonomy’] = []; in top of the function like this;
add_filter('acf/load_field/key=field_60c224887921d', 'load_just_categories_filter', 12, 1);
function load_just_categories_filter( $field ) {
$field['taxonomy'] = [];
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
//'hierarchical' => 1,
'parent' => 0
) );
if ( !empty($terms) ) {
foreach( $terms as $term ) {
$field['taxonomy'][] = 'category:'.$term->slug;
}
}
return $field;
}
The topic ‘Show only categories in the relationship field taxonomy filter’ 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.