Overview
In this tutorial will be described how, when searching for something from the category page, to limit the search results only to content from this category.
Standard WordPress search is far from idealand contains by default a very limited functionality which, over time, may not be enough to satisfy all yout needs. But fortunately this problem can be solved. You can use ready-made plugins or just manually add code snippets that append certain features. In this tutorial we will try to add one of the additional features. Namely - the limitation of search results by current category.
Suppose you have a search form on every page of your site. Including on the categories archive pages.
If you try to find something through this form, then you will be shown search results from all categories, not just from current. This is the standard behavior of the WordPress search form.
But this behavior can be changed. And then in the search results you will see only results related to the current page.
This tutorial will describe how to do this for the standard search form and for the Advanced Woo Search plugin form.
Limit search by category for standard WordPress posts
All you need to do to limit the search to a certain category is to add to the search query the 'cat' parameter with the value of category ID. To do this, in the search field add the corresponding new field of type 'hidden'.
This can be done by adding the following code snippet.
add_filter( 'get_search_form', 'my_get_search_form', 999999 ); function my_get_search_form( $form ) { if ( is_category() ) { $cat_id = get_queried_object_id(); $pattern = '/<form[Ss]*?>/i'; $add = '${0}<input type="hidden" name="cat" id="cat" value="'.$cat_id.'" />'; $form = preg_replace( $pattern, $add, $form ); } return $form; }
Limit search by category for WooCommerce products
For WooCommerce products search the code will be slightly different from what was used for posts search. You need to add in the search query parameter 'product_cat' with a value equal to the category ID for which you want to limit the search.
add_filter( 'get_product_search_form', 'my_get_product_search_form', 999999 ); function my_get_product_search_form( $form ) { if ( get_query_var('taxonomy') && get_query_var('taxonomy') === 'product_cat' ) { $cat_id = get_queried_object_id(); $pattern = '/<form[Ss]*?>/i'; $add = '${0}<input type="hidden" name="product_cat" id="product_cat" value="'.$cat_id.'" />'; $form = preg_replace( $pattern, $add, $form ); } return $form; } add_action( 'pre_get_posts', 'my_pre_get_posts_overwrite', 999 ); function my_pre_get_posts_overwrite( $query ) { if ( isset( $_GET['s'] ) && isset( $_GET['product_cat'] ) ) { $query->set( 'tax_query',array( array( 'taxonomy' => 'product_cat', 'terms' => $_GET['product_cat'], 'field' => 'ID', 'include_children' => true, 'operator' => 'IN' ) ) ); } }
Limit search by category for Advanced Woo Search plugin
For the Advanced Woo Search plugin it is also possible to do a search restriction by current category. And this can be done for ajax search, and for the search results page.
Just use the following code.
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; }
Comments