Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This snippet will exclude all products from any categories which you choose from displaying on the WooCommerce Shop page.
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
@eljkmw

This comment has been minimized.

Copy link

@eljkmw eljkmw commented Jan 29, 2018

When the 'clothing' category is a sub-category to, say, the 'women' category; the products in this clothing category don't get excluded on the Shop page.

@illadee

This comment has been minimized.

Copy link

@illadee illadee commented Jan 30, 2018

I would also like to know how to exclude products when it is a subcategory

@fattiechef

This comment has been minimized.

Copy link

@fattiechef fattiechef commented Feb 21, 2020

hello @stuartduff
can this function be used on a sngle php function script for multiple queries, example theme-functions.php or would you use on singular function script. example i have ancient-near-eastern to exclude buckles as it belongs to a different category like viking category. so can it be used mutliple times on same functions script or would you use category1-function.pho,category-2.php

@wmind

This comment has been minimized.

Copy link

@wmind wmind commented Jul 25, 2020

Hello there,
the code does work pretty well but it seems that this affects some of my other pages as well.
My site is a marketplace one and each seller has his own store page. These changes of hiding the categories takes effect on those pages as well.
How can I make it to be displayed only on a specific page?
I am new to php so I need some help please!

@marcelkraan

This comment has been minimized.

Copy link

@marcelkraan marcelkraan commented Oct 20, 2020

I like to use the code for an adult section that must be hidden from the home page.
But it must be possible to do an order? is that possible?

category "Adult" must be hidden from the main page. But when you search or click on an other page then it must be visible

Is that an option to fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment