
In a recent Business Bloomer Club Slack thread, a member inquired about hiding specific categories from the default WooCommerce shop page without creating a parent category. They still wanted to display products from those hidden categories on separate pages.
This is a common challenge for WooCommerce store owners who need more control over the products shown on their shop page. Fortunately, there are a few methods to hide categories from the default shop page while still showing their products in other areas of the store. Below, we’ll explore different solutions, including code snippets and shortcodes, to help you achieve this goal.
Filtering Categories Using pre_get_posts
One of the easiest ways to hide categories from the shop page is by filtering the main query using the pre_get_posts
action. This method allows you to exclude certain product categories from the shop page without affecting the display of those products on other pages.
Here’s a code snippet you can use to hide specific categories from the shop page:
function exclude_cats_from_shop_loop( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! function_exists( 'is_shop')) {
return;
}
if ( is_shop() ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'soda' ), // Replace 'soda' with the category you want to exclude
'operator' => 'NOT IN',
),
) );
}
}
add_action( 'pre_get_posts', 'exclude_cats_from_shop_loop' );
This snippet filters the main query for the shop page and excludes the category specified (in this case, “soda”). You can replace 'soda'
with the slug of any category you want to hide. The products in this category will still be accessible elsewhere on your site, but they will not appear on the default shop page.
Using WooCommerce Shortcodes
Another method is to use shortcodes to display products from the categories you want, while hiding the default shop view. By using shortcodes, you can create custom product displays for each category and page on your site.
For instance, you can use this shortcode to display products from a specific category:
[products category="soda"] // Replace with your desired category
This way, you can hide certain categories from the default shop page but still show their products elsewhere on your site. You can place this shortcode in any page or post to showcase products from a specific category.
Additionally, you can display featured products or products based on other attributes using other shortcodes. This flexibility allows you to create a custom shopping experience without altering the default shop page.
Conclusion
If you need to hide categories from the default WooCommerce shop page without creating a parent category, there are several solutions to consider. Using the pre_get_posts
filter is an effective method for excluding categories from the shop page, while shortcodes provide a simple way to display products from those hidden categories on separate pages.
By implementing these techniques, you can tailor your store’s product display to meet your needs while still ensuring that products from hidden categories are easily accessible to your customers.