Support

Account

Home Forums Backend Issues (wp-admin) Limit categories that can be assigned to a specific CF

Solved

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/

  • Wow, you rock. That was it!

  • 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?

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Limit categories that can be assigned to a specific CF’ is closed to new replies.