Site logo

Archived topic

Exclude Pages from Blog Search

3 replies · Started by Kulwant on January 11, 2019

Viewing posts 1–4 of 4

Hi,

I want to remove all pages and other types of pages from the search on my blog.

i.e when someone searches anything on my blog, they must see only posts.

Here I searched https://www.bloggingcage.com/?s=black+friday

It's showing all the pages too and some coupon archive pages too.

Hi there,

you can use the WP pre_get_posts query:

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'); 

https://docs.generatepress.com/article/adding-php/

It worked. :)

Thanks a lot.

You're welcome.

This archived topic is closed to new replies.