Mastering WooCommerce Category Filters: Displaying Only Main Categories in Sidebar Widgets
Efficient navigation is paramount for a successful online store. When customers can easily find what they're looking for, conversion rates improve significantly. However, a common challenge many WooCommerce store owners face, as highlighted in a recent support forum topic, is controlling the display of product categories in sidebar widgets.
The Challenge: Hiding Subcategories in WooCommerce Filters
The user in the forum thread perfectly articulated a frequent pain point: "I would like to display only the top-level categories—or hide the subcategories—using the WooCommerce Sidebar widget. Even though I have disabled the Show hierarchy option, all the subcategories still appear when the dropdown is opened." This scenario is common for stores with deep category structures where displaying every single subcategory in a prominent sidebar filter can overwhelm users and clutter the interface.
While the standard WooCommerce Product Categories widget offers a "Show hierarchy" option, its primary function is to indent subcategories, making their hierarchical relationship visually clear. Disabling it flattens the list but doesn't inherently prevent subcategories from appearing if they are part of the query. For a true "main categories only" display, especially in dropdown or list formats, a more targeted approach is required.
The Solution: Modifying the Category Query with Custom Code
To effectively display only top-level categories and completely hide subcategories in the WooCommerce Product Categories widget, you need to modify the arguments passed to the category query. This is achieved through a WordPress filter hook, which allows you to programmatically alter how the categories are fetched before they are displayed by the widget.
Step-by-Step Instructions to Implement the Solution:
This solution involves adding a small code snippet to your WordPress installation. It's crucial to follow these steps carefully.
- Access Your Theme Files:
- Connect to your website via FTP (FileZilla is a popular choice) or use your hosting provider's File Manager.
- Navigate to
wp-content/themes/. - Important: Always use a Child Theme for any code modifications. If you modify the parent theme directly, your changes will be lost when the theme is updated. If you don't have a child theme, create one or consider using a plugin like Code Snippets.
- Inside your child theme's folder, locate the
functions.phpfile.
- Add the Custom Code:
- Open the
functions.phpfile for editing. - Add the following PHP code snippet to the end of the file, before the closing
?>tag if one exists (it's often omitted in modern WordPress themes). add_filter( 'woocommerce_product_categories_widget_args', 'woo_show_main_categories_only_in_widget' ); function woo_show_main_categories_only_in_widget( $args ) { // This filter targets the WooCommerce Product Categories widget. // By setting 'depth' to 1, we instruct the query to retrieve only top-level categories. // 'parent' => 0 is often redundant when depth is 1 for top-level, but ensures clarity. $args['depth'] = 1; $args['parent'] = 0; return $args; }
- Open the
- Understand the Code:
add_filter('woocommerce_product_categories_widget_args', 'woo_show_main_categories_only_in_widget');: This line hooks into a specific WooCommerce filter. This filter allows us to modify the arguments used when fetching product categories for the widget.function woo_show_main_categories_only_in_widget( $args ) { ... }: This is our custom function that will be executed when the filter is applied. It receives an array of arguments ($args) currently used by the widget.$args['depth'] = 1;: This is the critical part. Setting thedepthargument to1tells the category query to retrieve only terms (categories) that are at the top level (i.e., they have no parent).$args['parent'] = 0;: This explicitly specifies that we are looking for categories whose parent is 0, which means they are top-level categories. Whiledepth = 1often implies this, settingparent = 0provides extra clarity and ensures only root categories are considered for the first level.
- Save and Test:
- Save the
functions.phpfile. - Clear any caching plugins you might be using.
- Visit your website's shop or category pages where the WooCommerce Product Categories widget is displayed. You should now see only the main, top-level categories, with all subcategories hidden, regardless of the widget's display style (list or dropdown).
- Save the
Alternative: Dedicated Filtering Plugins
For store owners who prefer a no-code solution or require more advanced filtering capabilities beyond just category depth, several premium WooCommerce filtering plugins are available. Plugins like WooCommerce Product Filters (by WooCommerce) or those from reputable developers often provide extensive options to control category display, including hierarchical depth, exclusion of specific categories, and various display styles, all through an intuitive user interface.
Best Practices and Considerations
- Always Use a Child Theme: As mentioned, modifying core theme files directly is risky. A child theme ensures your customizations are safe during theme updates.
- Backup Your Site: Before making any code changes, always perform a full backup of your website. This allows you to easily revert if something goes wrong.
- Test Thoroughly: After implementing the code, test your category filters on various pages and devices to ensure everything is working as expected and hasn't introduced any unintended side effects.
- Performance: This small code snippet is highly efficient and should have a negligible impact on your site's performance.
Conclusion
The problem of displaying only main categories in WooCommerce sidebar filters, as highlighted in the support forum, is a common one with a straightforward solution. By leveraging the woocommerce_product_categories_widget_args filter, store owners and developers can precisely control the category hierarchy displayed to their customers. This simple customization significantly enhances user experience, making your product catalog easier to navigate and ultimately contributing to a more successful online store.