3

I'm trying to exclude one category from the category list widget on the Shop and product archive pages for users other than the administrator. But for some reason it doesn't work.

Any help, thanks.

function exclude_category( $terms, $taxonomies, $args ) {
    $new_terms = array();

    /*if ( !is_admin()  && ( is_product() || is_shop() || is_product_category() || is_product_tag() ) ){ */
    if ( !is_admin() || is_product() || is_shop() || is_product_category() || is_product_tag()  ){  
        foreach ( $terms as $key => $term ) {
            if( is_object ( $term ) ) {
                if ( 'some-category' == $term->slug && $term->taxonomy = 'product_cat' ) {
                    unset($terms[$key]);
                }
            }
        }
    }
    return $terms;
}
add_filter( 'get_terms', 'exclude_category', 10, 3 );

I was inspired by this: https://stackoverflow.com/a/48849931/16275280

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Р. Р.
  • 75
  • 7

1 Answers1

3

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_userDocs 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:

enter image description here


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')
Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • 1
    Thank you so much! – Р. Р. Feb 02 '22 at 21:22
  • Can I exclude several categories in this way? `if ( array ( 'uncategorized', 'decor', 'hoodies' ) == $term->slug && $term->taxonomy = 'category')` – Р. Р. Feb 02 '22 at 22:11
  • Not a problem! You could use an array but not exactly like that! I've updated my answer. Please read the last section of my answer. – Ruvee Feb 02 '22 at 22:21
  • Thank you, that helped. Can I ask you to take a look at my question from another thread? Please https://stackoverflow.com/a/70827321/16275280 – Р. Р. Feb 02 '22 at 22:41
  • Sure I will when I get a chance! Looks like there are lots of details involved. – Ruvee Feb 02 '22 at 22:45
  • I have a problem. When I use your code, the seller data disappears on the product pages. – Р. Р. Feb 04 '22 at 20:40
  • The code I provided you with, executes only inside the terms/categories filter. So, what is a seller? Are you using `dokan` plugin on top of woocommerce? What does a category filter have to do with a seller data? If you think it's related to the snippet code I provided you with or you need assistance, please explain every single details so that I'll try to help you debug your app! – Ruvee Feb 04 '22 at 21:04
  • I think this is going to be very difficult, so I'll try to figure it out on my own for now. – Р. Р. Feb 04 '22 at 22:17