Created
April 5, 2018 22:25
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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