Hide Products From Shop Page by Category

This is for when you want to hide products of a certain category from displaying on the WooCommerce shop page. It removes products of a specific category from the shop page.

In this example, the product category to be removed is ‘packages’. You can change packages on line 12 to your own product category (slug), or a list of comma-separated product categories.

/**
* Hide the sponsorship package from the shop page
*/
function custom_pre_get_posts_query( $q ) {
 
	if ( ! $q->is_main_query() ) return;
	if ( ! $q->is_post_type_archive() ) return;
	if ( ! is_admin() && is_shop() ) {
		 $q->set( 'tax_query', array(array(
		'taxonomy' => 'product_cat',
		'field' => 'slug',
		'terms' => array( 'packages' ), // Don't display products in the packages category on the shop page
		'operator' => 'NOT IN'
		)));
	}
 	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

See more:

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]