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

wpcanyon.com

Tag: Visibility

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

WooCommerce: Hide out‑of‑stock products from the catalog

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Hide out‑of‑stock products from the catalog

WooCommerce: Hide Out-of-Stock Products from the Catalog

If you run a WooCommerce store, displaying out-of-stock products can frustrate customers and clutter your catalog. The quick fix is to hide these unavailable products, improving user experience and focusing attention on what’s actually purchasable.

Quick Fix: Hide Out-of-Stock Products

  1. Go to your WordPress admin dashboard.
  2. Navigate to WooCommerce > Settings > Products > Inventory.
  3. Check the box labeled Hide out of stock items from the catalog.
  4. Save changes.

This built-in option immediately removes out-of-stock products from your shop and category pages without any coding.

Why This Happens

By default, WooCommerce shows all products regardless of stock status. This is to maintain SEO value and product visibility, but it can lead to poor user experience when customers find products they cannot buy. Hiding out-of-stock items ensures your catalog only features available products, reducing confusion and potentially increasing sales.

Step-by-Step: Hide Out-of-Stock Products with Code Snippets

If you want more control or the built-in option is insufficient (for example, to hide out-of-stock products only on specific pages), you can use custom code snippets.

  1. Backup your site before making code changes.
  2. Add the following snippet to your child theme’s functions.php file or a site-specific plugin:
function custom_hide_out_of_stock_products( $query ) {
    if ( ! is_admin() && $query-is_main_query() && ( is_shop() || is_product_category() || is_product_tag() ) ) {
        $meta_query = $query-get( 'meta_query' );
        if ( ! $meta_query ) {
            $meta_query = [];
        }
        $meta_query[] = [
            'key'     ='_stock_status',
            'value'   ='outofstock',
            'compare' ='!=',
        ];
        $query-set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'custom_hide_out_of_stock_products' );

This snippet modifies the main WooCommerce product queries on shop, category, and tag pages to exclude products marked as outofstock.

Common Pitfalls

  • Cache Issues: Changes may not appear immediately due to caching plugins or server cache. Clear all caches after applying settings or code.
  • Theme or Plugin Conflicts: Some themes or plugins override WooCommerce queries, which can prevent hiding out-of-stock products. Test with default themes or disable conflicting plugins.
  • Incorrect Hook Usage: Using the wrong hook or targeting the wrong query can cause the code not to work. Always use pre_get_posts for modifying product queries.
  • Stock Management Disabled: If stock management is off, WooCommerce won’t track stock status, so hiding out-of-stock products won’t work.

Works on

  • Web servers: Apache, Nginx, LiteSpeed
  • Hosting panels: cPanel, Plesk, and others
  • WooCommerce versions 3.0 and above
  • Compatible with most themes and plugins that respect WooCommerce query filters

FAQ

Q: Will hiding out-of-stock products affect SEO?
A: Hiding out-of-stock products removes them from catalog pages but does not delete them. They remain accessible via direct URLs unless you also change product visibility. This approach minimizes SEO impact but consider using 301 redirects if you remove products entirely.
Q: Can I hide out-of-stock products only on the shop page but show them in search results?
A: Yes. Modify the conditional in the code snippet to target only is_shop() and exclude other queries like search.
Q: What if I want to show a message instead of hiding out-of-stock products?
A: You can customize your theme’s product templates to display an “Out of stock” notice instead of hiding the product. This requires editing template files or using hooks.
Q: Does this method work with variable products?
A: Yes. WooCommerce treats variable products as in-stock if any variation is available. Out-of-stock variable products will be hidden if all variations are out of stock.
Q: How do I re-enable showing out-of-stock products?
A: Simply uncheck the “Hide out of stock items from the catalog” option in WooCommerce settings or remove the custom code snippet if used.
…
WooCommerce How‑tos

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