Home » WooCommerce: Show Only 1 Category @ Shop Page

WooCommerce: Show Only 1 Category @ Shop Page

by Tutor Aspire

Hello WooCommerce Customizers!

Today we take a look at the WooCommerce Shop page and specifically at how to show only the category you want (and exclude all the others). Some store owners may need this, you never know the weird questions you get asked!

WooCommerce: Removing All Categories But One From the Shop Page
WooCommerce: Removing All Categories But One From the Shop Page

PHP Snippet: Only Show Products Belonging to One Category @ WooCommerce Shop Page

/**
 * @snippet       Display Products From 1 Category @ Shop Page
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */

add_action( 'pre_get_posts', 'tutoraspire_only_one_category_woocommerce_shop' );

function tutoraspire_only_one_category_woocommerce_shop( $q ) {
   if ( ! $q->is_main_query() ) return;
   if ( ! $q->is_post_type_archive() ) return;
   if ( ! is_admin() && is_shop() ) {
      $q->set( 'tax_query', array( array(
         'taxonomy' => 'product_cat',
         'field' => 'slug',
         'terms' => array( 'black' ), // change 'black' with your cat slug/s
         'operator' => 'IN'
      )));
   }
}

Result: Show Specific Category @ Shop Page

Here's the Before/After picture :)
Here’s the Before/After picture 🙂

You may also like