How do I filter searches by category in Magento?

How do I filter searches by category in Magento?

October 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I filter searches by category in Magento?

How to Filter Searches by Category in Magento

As an e-commerce developer, you’re likely looking for ways to enhance your site’s search functionality. One key aspect is allowing users to filter their searches by category. In this guide, we’ll show you how to implement both basic and advanced search filters by category using Magento 2.

Filtering Searches by Category in Form-Mini

The form-mini search offers a quick way for users to find products. To enable filtering by category:

  1. Open your catalog_search_form_mini.xml layout file located at <Magento_root>/app/code/Magento/Catalog/view/frontend/layout.

  2. Add the following code inside the <referenceBlock name="form.mini.search.container"> block.

<block class="Magento\CatalogSearch\Block\Search" name="catalogsearch.form.mini.renderers" template="Magento_CatalogSearch::renderers.phtml">
    <arguments>
        <argument name="search_form_attributes" translate="label" xsi:type="array">
            <item name="action" xsi:type="string">/catalog/search/index</item>
            <item name="method" xsi:type="string">post</item>
        </argument>
        <argument name="search_result_count" xsi:type="number">0</argument>
    </arguments>
</block>
  1. Save and clear your Magento cache.

This will add the necessary renderer block for category filtering in the form-mini search.

Advanced Search Filter by Category

For a more sophisticated search experience, you can use the advanced search feature:

  1. Navigate to the advanced search page located at /catalogsearch/advanced.

  2. Observe that the page already includes a dropdown for selecting categories using the form.mini.search.renderers block we added earlier.

  3. If you want to customize this dropdown or add more fields, you can do so by modifying the catalogsearch_advanced_form.xml layout file in <Magento_root>/app/code/Magento/CatalogSearch/view/frontend/layout.

  4. Within the <referenceContainer name="form.advanced.search.container"> block, you can add new fields or modify existing ones using Magento’s form element structure.

Filtering Using Catalog Search Form

If you want to create a custom search form with category filtering:

  1. Create a new layout XML file, e.g., catalogsearch_custom_form.xml, and place it in <Magento_root>/app/code/Your_Module/view/frontend/layout.

  2. Add the following code inside the <referenceContainer name="content"> block:

<block class="Magento\CatalogSearch\Block\Search" name="your_module.search.form" template="Your_Module::search/form.phtml">
    <arguments>
        <argument name="search_form_attributes" xsi:type="array">
            <item name="action" xsi:type="string">/catalog/search/index</item>
            <item name="method" xsi:type="string">post</item>
        </argument>
        <argument name="search_result_count" xsi:type="number">0</argument>
    </arguments>
</block>
  1. Create the form.phtml template in <Magento_root>/app/code/Your_Module/view/frontend/templates/search, and design your custom search form.

Conclusion

Enabling category filtering for searches provides a better user experience by allowing customers to find products more efficiently. By leveraging Magento’s built-in blocks and layouts, you can easily implement both basic and advanced search filters without needing to create complex customizations.

Remember that proper testing is crucial after making any changes to ensure the functionality works as expected across different browsers and devices. For a deeper dive into Magento development best practices, consider exploring our Magento 2 Development Guide at Laravel Company.

Happy coding!

Magento 2 Catalog Cache Themes

Our network