How to Show Only One Category In WooCommerce

woocommerce show only one categoryAre you searching for a way to show only one category on the WooCommerce Shop page? In this brief tutorial, we will share how you can achieve this using a simple code snippet we created.

It is important to note that you need some coding skills when creating this guide. If you are not familiar with coding, we recommend contacting a qualified developer. This ensures that you do not break down your website.

Categorizing products in your online store is a great way to de-clutter your WooCommerce shop. They guide users to the page or product they are searching for.

WooCommerce allows you to display only products, only categories, or both products and categories together. However, it does not allow you to show only one category on the Shop page. This is why we created this post to help you out.

Let us get right into it.

Steps to Display Only One Category in WooCommerce

In this section, we will illustrate how you can limit the products to only those from specified product categories in your WooCommerce store.

Before you proceed, it is important to use a child theme when making any changes to the functions.php file. This ensures that your changes are not lost in case of any update.

Without wasting much time, here are the simple steps you need to follow:

  1. Log in to your WordPress website as an admin
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function to show products from only one category in WooCommerce.
  3. Add the following code to the php file:
[php] add_action( ‘woocommerce_product_query’, ‘njengah_limit_shop_categories’ );
/**
* Show products from specific product categories on the Shop page.
*/
function njengah_limit_shop_categories( $q ) {
$tax_query = (array) $q->get( ‘tax_query’ );
$tax_query[] = array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array( ‘parts’ ),
‘include_children’ => true,
);
$q->set( ‘tax_query’, $tax_query );
}

[/php]
  1. It is important to specify the slugs of product categories you want to show from in. To show products from different categories you would change this line to:
[php] ‘terms’ => array( ‘accessories’, ‘music’ ),
[/php]
  1. It is important to note that child (sub) categories of the specific category/categories will also be included.
  2. If you do not want the products from subcategories to be shown, change
[php] ‘include_children’ => true,
[/php]

To

[php] ‘include_children’ => false,
[/php]
  1. Here is the outcome:springs category

Conclusion

By now, we are sure that you can show products from one category in your WooCommerce store. All you need to do is paste the code in your child theme’s function.php file.

However, if you are not able to achieve the same result, we recommend contacting a qualified developer. If you are not careful, you might break down your website.

We hope that this post helped you to achieve your desired outcome.

Similar Articles

  1. 100+ Tips, Tricks & Snippets Ultimate WooCommerce Hide Guide
  2. WooCommerce Redirect After Checkout: Redirect to Custom Thank You Page
  3. How to Check If Product is Variable In WooCommerce
  4. How to Set Up WooCommerce Subscribe or Unsubscribe To Our Newsletter
  5. How to Get Best Selling Products In WooCommerce
  6. How to Remove Category from Product Page WooCommerce
  7. How to Show Dropdown Variations Dropdown On Shop Page WooCommerce
  8. How to Sort WooCommerce Categories for Better User Experience
  9. How to Add Subcategories WooCommerce
  10. How to Redirect User If Not Logged in WordPress » Page Redirect
  11. How to Add Information to the WooCommerce Shop Page
  12. How to Hide All Products From Shop Page in WooCommerce