GET FREE VERSION GET THE PRO VERSION

How to limit search results to the current active category

Learn how to show search results only from certain active product categories.

In this article

Overview

In this article we will cover one specific case - how to limit search results to one specific active category.

This can be useful when you are displaying search form inside product categories pages and want to limit search results to current category terms.

Search form inside product category archive page

Search form inside product category archive page

There are several ways how you can do this and below we will cover some of them.

Limit search results by using plugin settings ( limited )

First ( but limited ) option - use Search Results Filters settings.

With these settings it is possible to create different filtering rules for search results.

For now we are interested in rules that are based on product categories and current page type.
So we need to create something like that:

Current page archives -> Category -> equal to -> {category_name} AND
Product category -> equal to -> {category_name}

OR

Product -> equal to -> Any product

Search results filtering rules

Search results filtering rules


So we create a rule that will search results with category {category_name} when the current page is product category archive for {category_name} category.

Problem here is that we need to manually create rules for each product category. This can be problematic if we have many of them or they are updated often. Solution here will be to use a custom code snippet instead.

Limit search results with custom code snippet

Simply use the following code snippet to limit search results for the current category:

add_filter( 'aws_search_query_array', 'my_aws_search_query_array' );
function my_aws_search_query_array( $query ) {
    if ( isset( $_REQUEST['aws_tax'] ) && $_REQUEST['aws_tax'] === 'product_cat' && isset( $_REQUEST['aws_page'] ) && $_REQUEST['aws_page'] ) {
        global $wpdb;
        $term_id = $_REQUEST['aws_page'];
        $query['search'] .= " AND ( id IN (
            SELECT object_id FROM $wpdb->term_relationships
            WHERE term_taxonomy_id IN ( select term_taxonomy_id from $wpdb->term_taxonomy WHERE term_id IN ({$term_id}))
        ) )";
    }
    return $query;
}
 
add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup' );
function my_aws_searchbox_markup( $markup ) {
    $hidden = '<input type="hidden" name="type_aws" value="true">';
    $new_fields = '<input type="hidden" name="tax" value="'.get_query_var('taxonomy').'"><input type="hidden" name="page" value="'.get_queried_object_id().'">';
    $markup = str_replace( $hidden, $hidden . $new_fields, $markup );
    return $markup;
}

After adding this code snippet don't forget to clear the search results cache.

There are no other additional steps you need to take. As soon as the code is added - it will start working and all search results inside the product categories archive pages will be limited to the current product category.