Best Premium and Free WordPress Themes › Forums › Online Shop › Search by category doesn't work
- This topic has 4 replies, 3 voices, and was last updated 5 years, 10 months ago by ianstrack.
-
AuthorPosts
-
August 17, 2018 at 5:51 am #34065valeriuzParticipant
Hello,
I’ve noticed that selecting a category on the front page, for search, doesn’t change the search results. The search only checks for the keywords inserted in the search field, and doesn’t look at the selected category.
Can you tell me how to fix that ? I saw this happens also on the test site you preview for the online shop theme.
Thank you.
August 17, 2018 at 12:44 pm #34090acmethemesKeymasterDear Valeriuz,
By WooCommerce default, the search field will only work via keywords. Though, it can be changed through theme customization.
Best Regards!January 12, 2019 at 5:08 pm #43175ianstrackParticipantHello, I have the same issue. Can you please describe how to fix it? Love the theme so far. Thanks!
January 12, 2019 at 5:09 pm #43176ianstrackParticipantI found this one bit of code but it doesn’t work…
https://gist.github.com/saadwaseem/241f11293c0c2902365ef23894f27ff9
January 13, 2019 at 4:01 am #43203ianstrackParticipantI added this to the functions.php under Appearance>Editor and it solved all my trouble. It allows the search to use the categories, and it excludes any out of stock items, as well as displays the search results correctly.
/**
* 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’);
add_action( ‘pre_get_posts’, ‘iconic_hide_out_of_stock_products’ );
function iconic_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() ) {
return;
}if ( $outofstock_term = get_term_by( ‘name’, ‘outofstock’, ‘product_visibility’ ) ) {
$tax_query = (array) $q->get(‘tax_query’);
$tax_query[] = array(
‘taxonomy’ => ‘product_visibility’,
‘field’ => ‘term_taxonomy_id’,
‘terms’ => array( $outofstock_term->term_taxonomy_id ),
‘operator’ => ‘NOT IN’
);$q->set( ‘tax_query’, $tax_query );
}
remove_action( ‘pre_get_posts’, ‘iconic_hide_out_of_stock_products’ );
}
-
AuthorPosts
- You must be logged in to reply to this topic.