Support

Account

Home Forums General Issues Exclude terms from taxonomy type field

Solving

Exclude terms from taxonomy type field

  • Hi, I scanned the whole web and found some threads about this, but none of those solutions worked for me.
    For example here https://support.advancedcustomfields.com/forums/topic/exclude-taxonomy-terms-from-frontend-display/

    I added many variants of this code

    add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_args', 10, 2);
    
    function my_taxonomy_args( $args, $field )
    {
       $args['exclude'] = array(16, 15, 17); //the IDs of the excluded terms
        return $args;
    }

    also with specific field name

    add_filter('acf/fields/taxonomy/wp_list_categories/name=test_tax2', 'my_taxonomy_args', 10, 2);
        
        function my_taxonomy_args( $args, $field )
        {
                $args['exclude'] = array(64,65,66); //the IDs of the excluded terms
            return $args;
        }

    and with the query function instead of wp_list_categories
    But I always end up with all the terms in the taxonomy field.

    Is there any workaround to this?
    Thanks!

  • What field type are you using? Select, checkbox or radio?

    The filter you are trying to use is only available for checkbox and radio fields and will not work for select type fields.

    For select type fields you need to use acf/fields/taxonomy/query

  • Hi @hube2 thanks for your response!

    Yes, I’m implementing it in a checkbox, but ALL categories keep showing up in the checkbox selector.

    What I need is to remove 3 or 4 categories from a field.
    I also need to remove a couple of tags from another field. I thought it’d be easy to find a code to filter those out, but I couldn’t find any that work.

  • I’m not sure why your filter is not working, it seems like it should be working.

    Is it possible that there is another acf/fields/taxonomy/wp_list_categories filter that is interfering?

    Try using the field key variant of the filter.

    Try increasing the priority of your filter. If this corrects it then might be another active filter on this hook.

  • Wow!!!! that worked!!!!
    I swear I had no faith in that recommendation because I don’t have any other snippets regarding ACF.

    I gave the snippet a priority of 100 and voilĂ !!

    You are a savior @hube2 !!!
    Thanks a lot for your time and knowledge!

  • That would lead me to do a search of all of the code on the site to find out where that other filter is hiding because your filter is probably now interfering with the operation of that filter.

  • You must think I’m kidding, but now I set the priority to 1 and it keeps working.
    I will investigate further, but you fixed my problem and I appreciate it very much!

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

You must be logged in to reply to this topic.