WooCommerce is a powerful e-commerce plugin for WordPress that allows you to sell products and manage your online store efficiently. While WooCommerce provides a plethora of features and customization options, sometimes you may want to hide a specific product category from search results. Perhaps you have a category that’s not meant for public viewing or should only be accessible through specific links. In this article, we will explore how to achieve this by using a code snippet in WooCommerce.
Why Hide a Product Category from Search?
There can be several reasons why you might want to hide a product category from search results:
- Exclusive Access: Some product categories may contain items that are not available to the general public and should only be accessible to specific users or through specific links.
- Seasonal or Limited-Time Offers: If you have seasonal or limited-time product categories, you might want to hide them from search results when they are not relevant.
- Organization: Hiding specific categories can help you better organize your store and prevent clutter in search results.
- SEO Strategy: You may want to optimize your store’s SEO strategy by focusing on specific product categories and excluding others from search results.
Step-by-Step Guide
To hide a product category from search results in WooCommerce, follow these steps:
- Backup Your Website: Before making any changes, it’s essential to create a backup of your website to ensure you can restore it in case something goes wrong.
- Access Your WordPress Dashboard: Log in to your WordPress admin panel.
- Navigate to the Theme Editor: In the WordPress dashboard, go to “Appearance” and select “Theme Editor.”
- Select Your Theme’s Functions.php File: In the Theme Editor, you’ll see a list of theme files on the right. Look for the “functions.php” file and click on it to open it for editing. Make sure you are editing the functions.php file in your active child theme. Not using a child theme? Use the Code Snippets plugin to add the code.
- Add the Code Snippet:To hide a specific product category from search results, you can use the following code snippet. Replace ‘your-category-slug’ with the slug of the category you want to hide:
add_filter( ‘get_terms’, ‘majaid_get_subcategory_terms’, 10, 3 );
function majaid_get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( ‘your-category-slug’ ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
} - Save the Changes: After adding the code snippet, click the “Update File” button to save your changes.
- Clear Cache: If you are using a caching plugin, clear your cache to ensure that the changes take effect immediately.
- Test the Results: Perform a search on your WooCommerce store to ensure that the specified product category is now hidden from search results.
Conclusion
Hiding a product category from search results in WooCommerce can be achieved easily by adding a simple code snippet to your theme’s functions.php file. This gives you greater control over the visibility of your categories and allows you to create a more tailored and organized shopping experience for your customers. Remember always to back up your website before making any code modifications, and exercise caution when editing theme files to avoid potential issues.