Skip to content

Instantly share code, notes, and snippets.

@saadwaseem
Created April 5, 2018 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saadwaseem/241f11293c0c2902365ef23894f27ff9 to your computer and use it in GitHub Desktop.
Save saadwaseem/241f11293c0c2902365ef23894f27ff9 to your computer and use it in GitHub Desktop.
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