-
Halil
Hi,
I checked the forum to exclude pages from search results and found thisfunction exclude_post_types_from_search( $query ) { if ( is_search() && $query->is_main_query() ) { $query->set( 'post_type', 'post' ); } } add_action( 'pre_get_posts', 'exclude_post_types_from_search' );
It limits search results to posts. I tried changing the code so that the search result will exclude pages but show result from posts and other post types.
Could u give me a hand for the code? I’d like to exclude pages only.
-
Hi there,
Try this:
function exclude_post_types_from_search( $query ) { if ( is_search() && $query->is_main_query() ) { $query->set('post_type', array( 'post', 'product' )); } } add_action( 'pre_get_posts', 'exclude_post_types_from_search' );
-
Halil
thanks Yin but I still see pages in the search results.
-
In that case, go back to the original code and add all the post types you want to show instead of excluding pages.
I’ve updated the code, you can keep on adding the post types.
-
Halil
great, thanka a lot Ying
-
No Problem 🙂
Viewing 6 posts - 1 through 6 (of 6 total)
- You must be logged in to reply to this topic.