Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WooCommerce] Remove categories from shop and other pages
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
// to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('YOUR_PAGE_SLUG') ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'woo' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
// Replace "woo" with the product category slug of the category you need hidden
@roger-richards

This comment has been minimized.

Copy link

commented Mar 4, 2018

Works great! (many other, older, solutions didn't work for me.)

@iliepandia

This comment has been minimized.

Copy link

commented Mar 8, 2018

Thanks indeed!

@rohankhera18

This comment has been minimized.

Copy link

commented Mar 9, 2018

Hey!

I tried adding this to the theme custom code but it did not work. I'm probably doing something wrong.
Can you help me with this? I need the category "Instashop" to not be shown on the Shop page.
For your reference, here is the link to the shop page: https://goo.gl/jEvyyd

Thank You :)

@dnomad-serg

This comment has been minimized.

Copy link

commented Mar 29, 2018

Hi guys,

can anyone explain please where this code should go to? functions.php file or somewhere else?

@msalep

This comment has been minimized.

Copy link

commented May 3, 2018

Hello!

This worked for me, I just created a snippet with the code, modified it to hide the categories from the shop page as per instructions and was able to hide 4 categories in my case:

`add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
// to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'cat1','cat2','cat3','cat4' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}`

Thanks!

@andyhsu123

This comment has been minimized.

Copy link

commented May 7, 2018

Hey guys i am trying to use this code on my website but it seems to not be working. Is there a specific wordpress version that i have to be updated to?

@amkojok

This comment has been minimized.

Copy link

commented Jun 22, 2018

@rohankhera18 add it to your child theme's functions.php file (you can go there from file manager or ftp to create another version of the file so your site doesn't break if you're not familiar with wp editor.) then just follow the author's instructions by replacing is_page to is_shop and woo to your desired category.

@amkojok

This comment has been minimized.

Copy link

commented Jun 22, 2018

@andyhsu123 it should work with the latest wp versions 4.8 till 4.9.6 and php 5.6 till 7.2

@wordpressforumhelp

This comment has been minimized.

Copy link

commented Jul 11, 2018

Hi there

How would I remove the category CLEARANCE SALE from this page?
the CLEARANCE SALE slug is sale-scarves

https://tinyurl.com/ycj4w97x

Many thanks

@flistefliste

This comment has been minimized.

Copy link

commented Sep 28, 2018

Great, thanks

@ALPJeff

This comment has been minimized.

Copy link

commented Nov 9, 2018

Works nicely - appreciate the share!

@benbo86

This comment has been minimized.

Copy link

commented Nov 28, 2018

Works well, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.