Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Online Shop Wordpress theme by ACME Themes: Category does not work while searching for products.
Add following function to your theme's function.php and category will be counted while searching for products.
/**
* Adds Category taxonomy query to WP search query
* This addition will make the search more relevant
* @param type $query
*/
function search_filter($query) {
if($query->is_search()) {
// category terms search.
if (isset($_GET['product_category']) && !empty($_GET['product_category'])) {
$tax_query = array(
'taxonomy' => 'product_cat',
'terms' => array($_GET['product_category']),
'field' => 'id',
'operator' => 'IN',
);
$query->tax_query->queries[] = $tax_query;
$query->query_vars['tax_query'] = $query->tax_query->queries;
}
}
}
add_action('pre_get_posts','search_filter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment