A customer uses WooCommerce to power their online shop. They required a certain category to be available through the WordPress dashboard but the category wasn’t to display on the shop pages or any widgets. Here is the PHP required to achieve the outcomes. Just add the following to your functions.php.
Don’t show WooCommerce category on a page
The following PHP will exclude a category name or thumbnail from showing on your shop and other pages. This script filters the WordPress get_terms function.
add_filter( 'get_terms', 'organicweb_exclude_category', 10, 3 ); function organicweb_exclude_category( $terms, $taxonomies, $args ) { $new_terms = array(); // if a product category and on a page if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page() ) { foreach ( $terms as $key => $term ) { // Enter the name of the category you want to exclude in place of 'uncategorised' if ( ! in_array( $term->slug, array( 'uncategorised' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } return $terms; }
Don’t show WooCommerce category in widget
The following PHP will exclude a category name from showing in your category widget. This script filters the WooCommerce woocommerce_product_categories_widget_args hook.
add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' ); function organicweb_exclude_widget_category( $args ) { // Enter the id of the category you want to exclude in place of '30' $args['exclude'] = array('30' ); return $args; }
This code seems to generic. I want some of the categories to show, and some not to show. I don’t see where I need to place the categories I don’t want to show in this code.
Thanks.
@Sandy, in the code, where it says ” Enter the name of the category you want to exclude in place of ‘uncategorised’ ” enter your comma separated list of categories there
Works Perfect!! Thank you!
Hello, how hide category on other pages ex shop, product_category…
In this code is only: “is_page” … When I add && is_shop etc … don’t work 🙁
Thank you so much!!!! All the other tutorials I found didn’t work on any page other than the products page – I needed it removed from another page and this worked perfect!E
Hello – Could this code be used to hide certain categories for only some user roles?
Is it possible to use this to hide all categories, thus showing only the product and price. I know you would usually set this in woocommerce but the theme has been tweaked by developer to show product and category in the shop overriding the woocommerce settings.
Great snippet Gary!
Can the “don’t show category” snippet hide the entire product list item (li) from the loop for the chosen category? This page in specific: http://stage.lileo.co/best-selling/. Looking to hide all list item products from the “rentals” category (li.product_cat-rental-product or li.product_cat-rentals).
Thanks for your help!
Luke
works only on Home page not in /shop.
Can you fix it thanks
F.
if you change the line:
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() && is_page() ) {
to read
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() && ( is_page() || is_shop()) ) {
That should remove it from the shop page too
This change did not help, I can see this under /shop/ still
I had used the category name but should have used the slug … all good. thanks for the article
MRN, I am trying to so the same, but with no luck. What code exactly did you use? I want to remove the category thumbnails from appearing in the Shop homepage.
Thanks!
Great! while many other tips show only how to hide products of a category, this one helped me to hide the category thumbnail!.
Nice code man! I did a little modification, instead of is_page, I used is_product_category(), and instead of $term->slug, I used $term->term_id.
Tks, I really appriciate.
I just added the code to my website, and the unwanted category “uncategorized” disappeared from the shop home page, indeed. But it is still shown on the category widget in the sidebar or every product detail page. How can I disable this as well? I don’t want to have this unwanted category showing up just anywhere.
thanks for the code 😉
I’m after the same ..
I have a big problem that lasts a few days and I can’t resolve it.
My site doesn’t display category thumbnails on a shop page, nor does it accept them, although the images are normally uploaded to the gallery. The strange thing is that it has accepted a picture of one category, the rest not, although they are the same size and extension (jpg). I changed the theme, disconnected the plugins, installed the Regenerate Thumbnails plugin, changed Permissions but nothing helped.
WC version: 3.3.4 WP version: 4.9.4
Does anyone know how can I resolve this problem?
Sorry – I know you have anwered this already, but I can’t figure out how to hide more than one category. I tried both (‘anledning, kategorier, kollektioner’) and (‘anledning’,’kategorier’,’kollektioner’) – but either way my page goes into failure mode :'(
try this
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
Is there a way to hide the product category on a specific time. I don’t want to show the product category or product from 4pm to 10pm. any help? Thank you.
Is there a way to use your code to hide products with a certain “tag” attached to it? so not hide category’s. but hide products based on a tag attached to it.
Yes by setting this piece of code in an If statement “if time = between .. and .. then run the script of the example” (and then in good PHP ofcourse)
Hello, I have been hitting my head against this for a while and have tried 3 or 4 different snipits that have been posted. I just tried the one above to remove one category (ethnic-ornaments) from dev-wp.ornaments.com/shop. I replaced is_page with is_shop. I have tried using the category slug and its ID. I am just stuck. Nothing I have tried works. Can you point me in a direction that will help?
the only way i cant get this to work is to use the following code
add_filter( ‘get_terms’, ‘organicweb_exclude_category’, 10, 3 );
function organicweb_exclude_category( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on a page
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() &&
is_product_category() ) {
foreach ( $terms as $key => $term ) {
// Enter the name of the category id you want to exclude in place of ”
if ( ! in_array( $term->term_id, array( ‘112’ ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
my problem is i still want it to show in the widget in the side bar which it isn’t any help be great
Thanks so much ! It works perfectly !
by slug on latest Woo did not work, but above works fine
Rick Nash answer worked for me after several hours. I had to use several two codes since it does not appear from other categories or sub-categories. Using shop code and Nash code does work.
add_filter( ‘get_terms’, ‘organicweb_exclude_category’, 10, 3 );
function organicweb_exclude_category( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on a page
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() &&
is_product_category() ) {
foreach ( $terms as $key => $term ) {
// Enter the name of the category id you want to exclude in place of ”
if ( ! in_array( $term->term_id, array( ’91’ ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
add_filter( ‘get_terms’, ‘ts_get_subcategory_terms’, 10, 3 );
function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if it is a product category and on the shop page
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() &&is_shop() ) {
foreach( $terms as $key => $term ) {
if ( !in_array( $term->slug, array( ‘cursos-presenciales’ ) ) ) { //pass the slug name here
$new_terms[] = $term;
}}
$terms = $new_terms;
}
return $terms;
}
Thanks for support.