Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

WooCommerce: Exclude a category from the shop page

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Exclude a category from the shop page

WooCommerce: Exclude a Category from the Shop Page

If you want to hide a specific product category from the main WooCommerce shop page, this tutorial will show you how to quickly exclude that category using a simple code snippet. This is useful when you want to keep certain products available on your site but not visible on the primary shop listing.

Quick Fix

  1. Identify the slug of the product category you want to exclude.
  2. Add a custom function to your theme’s functions.php file or a site-specific plugin.
  3. Use the provided code snippet to modify the main WooCommerce query and exclude the category.
  4. Save changes and refresh the shop page to verify the category is hidden.

Why This Happens

By default, WooCommerce displays all published products on the shop page regardless of their categories. There is no built-in setting to exclude specific categories from appearing on the shop page. To customize this behavior, you need to modify the main product query using WordPress hooks to filter out products belonging to certain categories.

Requirements

  • Access to your WordPress theme files or ability to add custom code via a plugin.
  • Basic knowledge of editing PHP files.
  • WooCommerce installed and active.
  • Identify the category slug you want to exclude (found under Products > Categories).

Step-by-step

  1. Find the category slug:

    Go to WordPress Admin > Products > Categories. Locate the category you want to exclude and note its slug (e.g., hidden-category).

  2. Backup your site:

    Before editing any code, create a backup of your site or use a child theme to avoid losing changes after updates.

  3. Add the exclusion code:

    Edit your theme’s functions.php file or a custom plugin and add the following code snippet. Replace hidden-category with your actual category slug.

    function exclude_category_from_shop_page( $query ) {
        if ( ! is_admin() && $query->is_main_query() && is_shop() ) {
            $tax_query = (array) $query->get( 'tax_query' );
    
            $tax_query[] = array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => array( 'hidden-category' ), // Replace with your category slug
                'operator' => 'NOT IN',
            );
    
            $query->set( 'tax_query', $tax_query );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category_from_shop_page' );
    
  4. Save and test:

    Save the file and visit your WooCommerce shop page. The products from the specified category should no longer appear.

Common Pitfalls

  • Wrong category slug: Using the category name instead of the slug will not work. Always use the slug.
  • Code placed in the wrong file: Adding code to the wrong functions.php file (e.g., parent theme without a child theme) can cause issues on theme updates.
  • Cache issues: If you use caching plugins or server caching, clear caches to see the changes.
  • Conflicting plugins: Some plugins that modify queries may interfere with this code.
  • Not targeting the main query: The code only works if it modifies the main WooCommerce shop query.

Works on

  • Web servers: Apache, Nginx, LiteSpeed
  • Control panels: cPanel, Plesk, and others
  • WooCommerce versions 3.x and above
  • Any WordPress theme that supports WooCommerce

FAQ

Q: Can I exclude multiple categories at once?
A: Yes. Replace the 'terms' array with multiple slugs, like array('category-one', 'category-two').
Q: Will this hide the category from other pages like archives or widgets?
No. This code only affects the main WooCommerce shop page. You need additional customization to hide categories elsewhere.
Q: Can I exclude categories without editing code?
WooCommerce does not provide a built-in option to exclude categories from the shop page. You can use plugins that offer advanced product filtering or visibility controls if you prefer not to code.
Q: What if the code breaks my site?
Immediately remove the code via FTP or your hosting file manager. Always backup before making changes and test on staging environments.
Q: Does this affect SEO?
Excluding categories from the shop page does not remove them from your site or search engines. Ensure you manage category visibility thoughtfully to avoid duplicate content issues.
WooCommerce How‑tos Tags:Catalog, Visibility, WooCommerce

Post navigation

Previous Post: Fix “Maximum execution time of 30 seconds exceeded” in WordPress
Next Post: Disable emojis and embeds to cut requests

Leave a Reply Cancel reply

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

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com