12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Dec 15, 2020

How to show products from specific categories on the Shop page in WooCommerce

Sridhar Katakam

In most WordPress sites the Shop page displays all the products by default. If you are looking to limit the products to only those from specified product categories, simply add the following code snippet.

add_action( 'woocommerce_product_query', 'wpdd_limit_shop_categories' );
/**
 * Show products from specific product categories on the Shop page.
 */
function wpdd_limit_shop_categories( $q ) {
    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
		'taxonomy' => 'product_cat',
		'field' => 'slug',
		'terms' => array( 'decor', 'music' ),
		'include_children' => true,
    );

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

Specify the slugs of product categories you want to show from in

'terms' => array( 'decor', 'music' ),

To show them from a single product category, of say accessories, you would change the above line to:

'terms' => array( 'accessories' ),

Note that any products in child (sub) categories of the specific category/categories will also be included.

If you do not want the products from sub categories to be shown, change

'include_children' => true,

to

'include_children' => false,

Before:

After:

where

If you are using Oxygen here’s how you set up the Shop page:

  1. Install and activate Oxygen Elements for WooCommerce plugin.
  2. Create a Template named say, Shop. Set it to inherit the design from your Main Catch All Template. Set the Template to apply to product post type’s archive.
  3. Edit with Oxygen.
  4. Add a Section and inside that add Products List component.
  5. Save.

References

https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/

https://github.com/luetkemj/wp-query-ref

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right