Search results on blog page

dear support,

how to limit the search on the blog page only to blog content? i don’t want search results from the whole site on the blog page. but i need also the site wide search on other pages.

thanks a lot and best wishes, kai

Hi There,

Please add the following code under functions.php file locates in your child theme:

function search_filter($query) {
	if ( !is_admin() && $query->is_main_query() ) {
		if ($query->is_search) {
			$query->set('post_type', 'post');
		}
	}
}
add_action('pre_get_posts','search_filter');

Hope it helps :slight_smile:

hi thai,

thanks a lot!

but this will change the general function of the search widget. right?
so when i use the same standard wp search widget on static pages and on the blog page your code will affect all searches. right?

thanks a lot! kai

Hi @iTurtle,

I say yes, because of all searches uses the same query and same result template. Unless if you’ll add the additional condition, like skip it if it’s product search.

Thanks!

hi rad,
thanks for your explanation.
so my question is if i can have a different search only on the blog page. maybe just filter out the static pages from the search result. or only search the blog articles.
is it possible to have 2 different search widgets?
thanks again! kai

Hello There,

Yes you may be able to use two separate search widget. You can use the default WP search widget and the WooCommerce Product search widget. Perhaps this can help: https://wordpress.org/plugins/search/product+search/

Regards.

hi ruenel!
thanks for your help! but i am not looking for a different product page search. i am looking for a different blog page search… i don’t think i can use the woocommerce plugins for that!?
thanks again! kai

Hi,

Regretfully what you are trying to achieve could only be possible with custom development, but this would be outside the scope of support we can offer. You may wish to consult a developer to assist you with this. X is quite extensible with child themes, so there are plenty of possibilities.

Thanks for understanding. Take care!

hi paul,
thanks a lot!
i actually found a plugin which does exactly what i needed :wink:
it took me many days to find it, though.
best wishes, kai

You’re most welcome! And I’m glad you found one :wink:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.