[Support request] Exclude Pages from Blog Search

Home Forums Support [Support request] Exclude Pages from Blog Search

Home Forums Support Exclude Pages from Blog Search

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #779117
    Kulwant

    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.

    #779256
    David
    Staff
    Customer Support

    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/

    #779258
    Kulwant

    It worked. 🙂

    Thanks a lot.

    #779261
    David
    Staff
    Customer Support

    You’re welcome.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.