Hide WooCommerce Products From Specific Categories

Hide WooCommerce Products From Specific Categories

If you want to hide specific products from different categories on your WooCommerce site, are you looking for a simple way? Then you are in the right place! I have searched in the WordPress plugin directory and found some plugins available. For such a simple task I didn’t prefer to use a plugin. You can implement this quickly even if you haven’t better coding knowledge. So let’s get started!

Step 01: Login to your WordPress Dashboard

Step 02: Go to Appearance >Theme Editor

Step 03: Select your theme from the right side.

Step 04: Click functions.php from Theme Files

Step 05: Copy and paste the below code on the file and hit the Update button

// Hide Products From Specific Categorys
function custom_pre_get_posts_query( $q ) {
	$tax_query = (array) $q->get( 'tax_query' );

	$tax_query[] = array(
		'taxonomy' => 'product_cat',
		'field' => 'slug',
		'terms' => array( 'uncategorized'), // Don't display products in	uncategorized category on the shop page.
		'operator' => 'NOT IN'
	);

	$q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

That’s it 🙌

If you have any query then please let me know in the comments section. I have checked this code with WooCommerce latest version. If you think this post saved your time, please subscribe to our newsletter for regular updates.

Happy coding 🙂

Thank you in advance!


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *