Archived topic
Search not working
10 replies · Started by Scott on April 4, 2023
Dunno if this is a theme issue or a WP issue. But the ONLY search that works is the Woocommerce Product Search block and ONLY returns products. No other search block returns anything at all and just leads to a 404 page (which ironically has a search feature that also returns nothing). The navigation search thing also returns nothing.
/wp-content/themes/generatepress/assets/js/menu.min.js
/wp-content/themes/generatepress/assets/js/navigation-search.min.js
/wp-content/gp-premium/menu-plus/functions/js/offside.min.js
have all been excluded from WP-Rocket optimizations.
So I can search products … but Pages & Posts are not searched.
hi there,
I just tested the search2 link you attached, and it works normally.
Have you tried testing it on another device?
Let me know!
No I haven't but I will and report back.
Doesn't work on a different device or different web connection.
Can you disable all plugins except Woo and GP Premium?
If you are using a child theme, can you switch to the parent theme?
Let me know how it goes.
I found the issue. It’s this code snippet used to hide a product category from search results.
/* Remove Product Category from Search */
function remove_cat_from_search ( $q ) {
if (!is_admin() && $q->is_main_query() && $q->is_search()) {
$q->set( 'post_type', array( 'product' ) );
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'castlerockrehab','medicalsolutions' ), //no t-shirts in search
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'remove_cat_from_search' );
which really sucks as I need this functionality 😔
Hi Scott,
To clarify, do you also want Posts to appear aside from Products when searching?
Ideally posts, pages, and products is what I’d prefer.
Can you try replacing the code you have with this?:
add_action( 'pre_get_posts', function( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'page', 'post', 'product' ) );
$tax_query = (array) $query->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'castlerockrehab','medicalsolutions' ), //no t-shirts in search
'operator' => 'NOT IN'
);
$query->set( 'tax_query', $tax_query );
}
} );
Like a charm. This isxwhy you guys and gals are the bestest!
You're welcome, Scott!