Home › Forums › Backend Issues (wp-admin) › Limit categories that can be assigned to a specific CF
Hi,
I have 10 categories in my site and a custom field group. Now, I need that authors could assign only one out of 4 specific categories to that custom field, while the other are for generic posts.
Currently, in the backend ACF form, authors are able to assign any category to the custom field. How can I limit their choice only to the specific 4 categories?
Thanks.
You can use this filter https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-query/. See get terms arguments here https://developer.wordpress.org/reference/classes/wp_term_query/__construct/. You and use include to only list the ones you want or you can use exclude to eliminate the ones you don’t want.
Hi,
thank you for answering
Will this filter show only the categories I want into the category meta box in the admin backend, for the specific field group?
Fabio
This would only work for an ACF taxonomy field and not for the standard WP category meta box.
I don’t know if you can limit the categories in the WP meta box, after a quick search I found nothing on the subject. The only thing I can find information on is how to hide it.
Been struggling with this as well. Based on the above using the following in hopes of having my ACF category field only show two categories, the ones with an id of 10 and 16. The field name is “event-category”. When using the below in functions.php, it does not take any affect so assuming I’m missing something. Any thoughts? Also is there a way to set either 10 or 16 to be checked by default on new posts in the custom post type?
function training_taxonomy_query( $args, $field, $post_id ) {
$args[‘include’] = ‘10,16’;
return $args;
}
add_filter(‘acf/fields/taxonomy/queryname=event_category’, ‘training_taxonomy_query’, 10, 3);
You’re missing a slash
'acf/fields/taxonomy/query/name=event_category'
https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-query/
Looks like I spoke too soon. The above works perfectly if the taxonomy is set a select menu (only shows the 2 categories), but if i set to checkbox, radio buttons, etc, it lists all the categories rather than just the two.
Is this a bug, or do I need to add something extra? Also is there anyway to set a default category value for new posts with this method?
Hi,
I would appreciate help. I tried above solution with small modyfication – I want to exclude one category from front-end form, category ID is 158, acf field slug – tematyka
I put it on the functions.php of my theme:
function blokada_kategorii( $args, $field, $post_id ) {
$args[‘exclude’] = ‘158’;
return $args;
}
add_filter(‘acf/fields/taxonomy/query/name=tematyka’, ‘blokada_kategorii’, 10, 3);
And I get error:
Parse error: syntax error, unexpected ‘=’, expecting ‘,’ or ‘)’ in [shortened]/functions.php on line 202
Line 202 is where add_filter… is
What I did wrong? Or is any other method to exclude one category from the field?
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!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.