Archived topic
Wordpress default search vs. Woocommerce Search
6 replies · Started by Anonymous on February 27, 2016
Hi Tom,
I want to display in the Wordpress deafult search only posts and I added this code into my function.php file.
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
However, adding the above code has an impact on my woocmmerce search form as the search form in Woocomerce does not display any results.
Can you help me further?
This is the search results of my Wordpress default search
http://www.kaufpiraten.de/?s=test
and this of my Woocommerce search result
http://www.kaufpiraten.de/?s=jeans&post_type=product
Thanks.
Lihas
Hi there,
Give this a shot:
function SearchFilter($query) {
if ( $query->is_search ) {
$query->set( 'post_type', 'post' );
}
if ( function_exists( 'is_woocommerce' ) ) :
if ( $query->is_search && is_woocommerce() ) {
$query->set( 'post_type', 'product' );
}
endif;
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Hi Tom,
this did not solved my issue.
The following search is not working, altough I have woocommerce products labeled as jeans in my store.
http://www.kaufpiraten.de/?s=jeans&post_type=product
The default Wordpress search is working.
Lihas
I just tested this without any functions at all and it worked: my-site.com/?s=ninja&post_type=product
If you remove all of the functions above, does it not work?
Hi Tom,
I solved it. I have to add the following condition "&& !is_woocommerce()" in the first if statement. This is the code I have used and it works.
function SearchFilter($query) {
if ( $query->is_search && !is_woocommerce()) {
$query->set( 'post_type', 'post' );
}
if ( function_exists( 'is_woocommerce' ) ) :
if ( $query->is_search && is_woocommerce() ) {
$query->set( 'post_type', 'product' );
}
endif;
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Thanks a lot.
Lihas
Hi Tom,
this is the right code. Otherwise your search function in the admin area will not deliver any results.
function SearchFilter($query) {
if ( $query->is_search && !is_woocommerce() && !is_admin()) {
$query->set( 'post_type', 'post' );
}
if ( function_exists( 'is_woocommerce' ) ) :
if ( $query->is_search && is_woocommerce() && !is_admin()) {
$query->set( 'post_type', 'product' );
}
endif;
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Awesome! Thanks for posting the solution :)
