- This topic has 6 replies, 3 voices, and was last updated 7 years, 3 months ago by
Tom.
-
AuthorPosts
-
February 27, 2016 at 8:10 am #175695
Lihas
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
-
This topic was modified 7 years, 3 months ago by
Tom.
February 27, 2016 at 10:07 am #175721Tom
Lead DeveloperLead DeveloperHi 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');
February 28, 2016 at 3:46 am #175868Lihas1987
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
February 28, 2016 at 11:11 am #175926Tom
Lead DeveloperLead DeveloperI 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?
March 3, 2016 at 3:36 am #176774Lihas1987
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
March 3, 2016 at 5:38 am #176795Lihas1987
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');
-
This reply was modified 7 years, 3 months ago by
Tom.
March 3, 2016 at 10:28 am #176828Tom
Lead DeveloperLead DeveloperAwesome! Thanks for posting the solution 🙂
-
This topic was modified 7 years, 3 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.