Excluding a product category on WooCommerce
Use the following conditional checks instead:
- To check whether a user is admin or not, you could use the
roles
property of the user object returned from wp_get_current_user
Docs function.
- To check whether you're on woocommerce pages or not (i.e
is_product() || is_shop() || is_product_category() || is_product_tag()
), you could simply use this is_woocommerce()
function.
So the entire code would be:
add_filter('get_terms', 'exclude_category', 10, 3);
function exclude_category($terms, $taxonomies, $args)
{
$roles = wp_get_current_user()->roles;
if
(
!in_array('administrator', $roles)
&&
is_woocommerce()
)
{
foreach ($terms as $key => $term)
{
if (is_object($term))
{
if ('decor' == $term->slug && $term->taxonomy == 'product_cat')
{
unset($terms[$key]);
}
}
}
}
return $terms;
}
Note:
- I've used
'decor'
as the category slug to test this snippet on my site, so don't forget to replace it with your own category slug.
And here's the result:

Excluding a category on Wordpress
add_filter('get_terms', 'exclude_category', 10, 3);
function exclude_category($terms, $taxonomies, $args)
{
$roles = wp_get_current_user()->roles;
if (
!in_array('administrator', $roles)
)
{
foreach ($terms as $key => $term)
{
if (is_object($term))
{
if ('uncategorized' == $term->slug && $term->taxonomy == 'category')
{
unset($terms[$key]);
}
}
}
}
return $terms;
}
Note:
- I've used
'uncategorized'
as the category slug to test this snippet on my site, so don't forget to replace it with your own category slug.
Excluding multiple categories in WooCommerce
You could set up an array and use in_array
function for the conditional check. Like this:
$excluded_cat_slugs = array ('hoodies', 'decor');
if(in_array($term->slug, $excluded_cat_slugs) && $term->taxonomy == 'product_cat')
Excluding multiple categories in Wordpress
$excluded_cat_slugs = array ('uncategorized', 'test-category');
if(in_array($term->slug, $excluded_cat_slugs) && $term->taxonomy == 'category')