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:
- Install and activate Oxygen Elements for WooCommerce plugin.
- Create a Template named say,
Shop
. Set it to inherit the design from your Main Catch All Template. Set the Template to apply toproduct
post type’s archive. - Edit with Oxygen.
- Add a Section and inside that add Products List component.
- Save.