Support

Account

Home Forums ACF PRO Exclude taxonomy terms from frontend display

Solved

Exclude taxonomy terms from frontend display

  • Is it possible to exclude some terms from a taxonomy list on a frontend form and if so how might I do that?

    I have a form which creates a new post, but there are one of two categories I’d like to hide from public view.

    Thanks

  • Solved it with a filter.

    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;
    }
  • Dear Leanda,

    Thank you for sharing. This works for me as a filter to exclude some categories from the field in the backend (I was looking for this, to give editors less choice when selecting categories for posts). But, I was looking for a way to do this on a field by field basis. Say, for the taxonomy field “test_tax1” exclude (or hide) the categories 64,65,66 and for the taxonomy field “test_tax2” exclude (or hide) the categories 67,68.

    Can you think of a way to do this?
    At the moment the categories are excluded (hidden) on all taxonomy fields of the regular wp categories.

  • I think I found a solution. For anyone interested:

    <?php
        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;
        }
        ?>
  • Glad you found a solution, that might just come in handy for me on an upcoming project. Thanks for sharing.

  • I tried both leanda version and Florian version – none worked. I got no error, just category is still visible in frontend form. What I try to achieve: exclude category with ID 158 from being visible in frontend form, slug of this field is “tematyka”. So my code is:

    <?php
    add_filter(‘acf/fields/taxonomy/wp_list_categories/name=tematyka’, ‘my_taxonomy_args’, 10, 2);

    function my_taxonomy_args( $args, $field )
    {
    $args[‘exclude’] = array(158); //the IDs of the excluded terms
    return $args;
    }
    ?>

    I tried also without name, to exclude this category from all fields and forms (though I have only one form):

    <?php
    add_filter(‘acf/fields/taxonomy/wp_list_categories’, ‘my_taxonomy_args’, 10, 2);

    function my_taxonomy_args( $args, $field )
    {
    $args[‘exclude’] = array(158); //the IDs of the excluded terms
    return $args;
    }
    ?>

    None worked. What did I wrong?

  • Is it also possible to hide certain categories from being selected in the backend taxonomy field display?

  • The hook add_filter('acf/fields/taxonomy/wp_list_categories/name=test_tax2' will work on the front end an back end unless you put something in your code to prevent it. For example:

    
    // not in the admin
    if (is_admin()) {
      return $args;
    }
    
  • This is my filter function. Its not working. Here category is the custom field with taxonomy type and its called category. It is supposed to pull all categories from “product_cat”.

    
    add_filter('acf/fields/taxonomy/wp_list_categories/name=category', 'exclude_taxonomy_args', 10, 2);
    function exclude_taxonomy_args( $args, $field ) {
      $args['exclude'] = array(19,27,28); //the IDs of the excluded terms
      return $args;
    }

    After adding this, I still see the categories mentioned in the exclude above.

  • I am actually using the select type for the categories list. Found my answer here. I had to use query instead of wp_list_categories

    https://support.advancedcustomfields.com/forums/topic/add_filteracffieldstaxonomyquery-bug/#post-85106

  • @hube2 Is there anyway to specify the category slug in the array instead of the id?

  • No. ACF uses get_terms() https://developer.wordpress.org/reference/functions/get_terms/. Arguments use supply are those used by that function.

  • Answering my own question above. This is how you can exclude categories based on slug. In my case, I have a multi-site and category id may change. Following example is to hide the Uncategorized category. (I’d like to keep it as the default one and not rename it for my own reasons). Hope that helps somebody in the same situation.

    // get term id's
    add_action( 'init', 'get_term_ids' );
    function get_term_ids() {
        global $uncategorized_id;
        $u = get_term_by( 'slug', 'uncategorized', 'product_cat' );
        $uncategorized_id = $u->term_id;
    }
    
    // exclude categories from category dropdown
    add_filter('acf/fields/taxonomy/query/name=category', 'exclude_categories', 10, 2);
    function exclude_categories( $args, $field ) {
      global $uncategorized_id;
      $args['exclude'] = array($uncategorized_id); //the IDs of the excluded terms
      return $args;
    }

    Thanks everyone!

  • have been trying this code – the only thing I changed are the Term IDs but I can still see them. Am I supposed to be changing something else, as well?

  • Same here, tried all of the above filter, but the categories are still showing in the frontend form.
    Did something change since this was working?

  • Hi, I had to purchase a plugin which did the trick. They haven’t updated it and gave me issues with my site so I’ve been using a previous version 0.8.8.9:

    https://www.acf-extended.com

  • Oh! I see, Thanks for the heads up.

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

The topic ‘Exclude taxonomy terms from frontend display’ is closed to new replies.