• Skip to main content
  • Skip to primary sidebar
  • Business
  • WordPress
  • Security
  • Marketing
  • Publicity
  • Computing
  • Resources
  • Whatevs

Purely Wordpress

Just The Tech, News, and Info We Deem To Post

  • Home
  • About
  • Contact
  • Contribute
You are here: Home / Code Snippets / Prevent product category from displaying on WooCommerce shop page

Prevent product category from displaying on WooCommerce shop page

Woocommerce Code Snippets

Add the following code to your theme’s function.php file, or a functionality plugin to modify the WooCommerce Shop Page in order to prevent specific categories from displaying on the WooCommerce Shop Page.

<?php

/** WooCommerce Hide Cat. */
/** This code should be added to functions.php of your theme or functionality plugin.  **/
/** DO NOT INCLUDE THE OPENING <?PHP **/

function custom_pre_get_posts_query( $q ) {
    $tax_query = (array) $q->get( 'tax_query' );
    
    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'your category' ), 
            // Hide products from "your category" on the shop page.
           'operator' => 'NOT IN'
    );

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

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

by Jack Alltrade on October 16, 2020

Filed Under: Code Snippets Tagged With: Code, Customization, Shop Page, WooCommerce

Reader Interactions

Share Your Two Cents Cancel reply

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

Primary Sidebar

Categories

  • Code Snippets
  • Development
  • File Management
  • Genesis Framework
  • Hosting
  • Legacy
  • Plugins
  • SEO
  • Tech Terms
  • Themes
  • User Experience
  • User Interface
  • WordPress Core
  • WordPress Functionality

Warning! Use at your own risk!

As always, use at your own risk and remember to backup your site prior to inserting new code.

© Copyright Jack Alltrade & Associates 2025 · Purely Supplemental™ is a trademark of Jack Alltrade & Associates