Categories
Uncategorised

How to hide WooCommerce category from the shop page

WooCommerce is a fantastic ecommerce plugin that lets you turn any WordPress website into an online shop.

You can create multiple categories for your products, and all of these products, regardless of their category, are listed on the shop page (www.yoursite.com/shop/).

Many shop owners want the ability to hide certain products from the shop page – for example for special products which may only be linked via a newsletter.

How to hide a category from the shop archive page?

In order to create a hidden category for your WooCommerce shop, you can include this code in either your functions.php file, or add it into a custom plugin. Or you can check out our plugin woocommerce hide product from shop page.

Step 1: Create the new hidden category within WooCommerce. You can call it whatever you want. This is done using a function which inserts our new term into the WooCommerce product taxonomy (product_cat)

function create_hidden_cat() {
	wp_insert_term(
		'Hidden',
		'product_cat',
		array(
			'description' => 'Hidden products',
			'slug'        => 'Hidden',
		)
	);
}
add_action( 'init', 'create_hidden_cat' );

Step 2: Amend the shop query to hide our new category. You can add multiple hidden categories into the ‘terms’ array if you wish.

function before_get_posts_query( $q ) {
    $tax_query = (array) $q->get( 'tax_query' );
    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'hidden' ),
           'operator' => 'NOT IN'
    );
    $q->set( 'tax_query', $tax_query );
}

Step 3: Add the action. This puts our new array into the product query.

add_action( 'woocommerce_product_query', 'before_get_posts_query' );

Once this is done, you will be able to add any of your products into the new category and they will be hidden from the Shop page. You can of course still link directly to your products.

If you are having difficulty please comment below. I have also put this code into a ready to use plugin which you can download here.

Leave a Reply

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