To remove a list of one or more product categories from the WooCommerce shop page, paste this snippet into your functions.php. Before it will work, you need to change the comma-separated list of product category IDs to the IDs you want to remove.
<php
/**
* Remove specific category ID's from the WooCommerce shop page
*/
add_action( 'pre_get_posts', 'uw_remove_product_cats_shop_page' );
function uw_remove_product_cats_shop_page( $query ) {
// Comment out the line below to hide products in the admin as well
if ( is_admin() ) return;
if ( is_shop() && $query->is_main_query() ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'ID',
'terms' => array( 200, 205, 210 ),
'operator' => 'NOT IN'
)
) );
}
}
Thank you, your code helped me.
I have used this code and found that it works, but also removes the hidden category from the “Products” view in WP Admin. The number of products shown includes the hidden ones, but they are not visible. Even if I select to show the category specifically, it comes back with no visible products even though it has parentheses indicating that there are products in that category.
Hello, Robert. You can add an additional check using
is_admin()
.I don’t know where that would go in the code.
I also have the problem of the products not appearing in the list of products in the wordpress backend but it does hide them from the main shop page. I added is_admin() in the function but that didn’t work.
if ( is_shop() && is_admin() && $query->is_main_query() ) {
Is this correct?
You need to use
! is_admin()
(not admin).Doesn’t work on mine, sorry. Where should I enter the cat’s ID in the code ?
Hi Aboo,
Please see this line of code from above full code :
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘ID’,
‘terms’ => array( 200, 205, 210 ),
‘operator’ => ‘NOT IN’
you should write your cat’s ID inside array. Number 200, 205 and 210 means the cat’s ID. You should replace with your cat’s ID.
Greetings, I’m starting with WordPress and WooCommerce, have added the function in my funtions.php theme file I’m using, but do not understand how in the loop summon WooCommerce. Thanks for the help.
Work great!
Now how to remove the pagination to make page completly blank?
I was struggling on this “easy task” forever. This should be a build in feature but it isn’t. Then I found this plugin and it works like a charm. Thank you very much. Higly recommended.
Thanks a lot. It helps me a lot.
Thanks for this code, but it’s not working for me. I replaced the numbers with the appropriate catID but it’s still showing up on my shop page. Perhaps it’s because I changed the name of my shop page to ‘give’?
I try the function, however it work on my english page, not in the french one.
I put the both ID number for each page.
There is a specific reason wi it doesn’t work for the french page (Origin language of the website)
For you information I use WPML multilanguage software.
I try with the slug too, and I got the same problem, I see the unwanted category in french.
I finally delete the category and install a new one and it works!
Hi! You can remove product categories from WooCommerce shop page (and site-wide, which means, from search results, categories page, pages with WooCommerce shortcodes, etc.) by using the WooCommerce Products Visibility plugin.
You can also choose the user roles to whom the products will be invisible.
The plugin is also WPML compatible, which means that the rules you define, will take place for all your site’s languages!
This will work
Hey Javed,
And what if the shop page has gotten a different forward and is now post id5 how do I use post with id instead of shop?