Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How To Limit Search Results For Specific Post Types in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How To Limit Search Results For Specific Post Types in WordPress

Have you ever wondered how you can limit your search results to specific post types? Its not very hard. We’ve already showed you how to disable the search feature in WordPress by modifying the functions.php file. Now we’re going to do the same thing except to filter our search results.

Open your functions.php file and add the following codes:

function searchfilter($query) {

    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('post','page'));
    }

return $query;
}

add_filter('pre_get_posts','searchfilter');

Notice the line that says

$query->set('post_type',array('post','page'));

You can filter the search results by changing the values in the array variable. Right now it is set to display posts and pages but you can modify it to display anything you want.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Steven says:

    I’ve got an easy function in my themes functions.php file, which should only filter the Posts by a search term… when I search something now, the HTTP 500 Error “The website cannot display the page” appears. Anyone got an Idea, whats wrong with my function?

    function searchFilter($query) {
    if ($query->is_search)
    {
    wp_reset_query();
    $args = array ( ‘s’ => $_GET[‘s’] );
    query_posts( $args );
    }
    }
    add_filter(‘pre_get_posts’, ‘searchFilter’);

  2. Greg says:

    This is restricting all search forms to the custom post type – including my sidebar searchform, which needs to return all results. This is working for me:

    function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
    if(isset($_GET[‘post_type’])) {
    $type = $_GET[‘post_type’];
    if($type == ‘book’) {
    $query->set(‘post_type’,array(‘book’));
    }
    }
    }
    return $query;
    }
    add_filter(‘pre_get_posts’,’searchfilter’);

    • Jonathan Joosten says:

      Thanks for the assist, I improved your code so people can only search allowed post_types.

      function searchfilter($query)
      {
      if ($query->is_search && !is_admin() )
      {
      if(isset($_GET[‘post_type’])) {
      $types = (array) $_GET[‘post_type’];
      $allowed_types = get_post_types(array(‘public’ => true, ‘exclude_from_search’ => false));
      foreach($types as $type)
      {
      if( in_array( $type, $allowed_types ) ) { $filter_type[] = $type; }
      }
      if(count($filter_type))
      {
      $query->set(‘post_type’,$filter_type);
      }
      }
      }
      }
      add_filter(‘pre_get_posts’,’searchfilter’);

      • Dan Sz. says:

        How is this implemented? If I’m reading Greg’s comment, correctly we want a single form that is limited to a post type, while keeping default search intact for other areas of the site.

        I’m asking because a site i’m working on needs a searchable “Resource Library”, which I’d like to build out with compromising the normal search functionality.

  3. Tadeu says:

    Hi, is it possible to limit search only to title, category and tags of posts?

  4. Sandeep says:

    Hi,

    I’m not able to restrict pages in search filter.
    I just need the search within the posts and not pages.

    $query->set(‘post_type’,array(‘post’));
    This displays pages too.

  5. Jared says:

    Just a note to add to this, you do not need to return $query.

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  6. Peter says:

    Thanks for the great tip!

  7. WPBeginner Staff says:

    Nick WordPress search functionality is not so great in finding content.

  8. Nick says:

    I used this code to restrict my search results to Pages, not Posts. It worked in that regard, however, it seems to only search the Page names as opposed to content. For instance, if a user searches “pricing”, the Pricing page will be a result but if they search “price” or “cost” (both of which are words on the pricing page) nothing is found. Is there a snippet of code I’m missing?

  9. emre says:

    Hi,
    I have a question and I couldn’t get a solution since last month.
    I have lost of categories, pages and re-directions in my blog so my search box finds many unnecessary results when you try to search something. I want to customize my search.php for only categories part. In other words, we you search something, the results should be only from categories sections. So I will be get rid of redundant & duplicated results. My current codes are as below…Please help me :)

  10. sam says:

    How to limit by specific category? thanks

  11. Hasan Gad Allah says:

    Thank you , i was looking for that code (Y) :)

  12. Felix says:

    There’s one problem with your snippet:

    It limits the search results in the backend, you should wrap it with:


    if ( !is_admin() ) {
    // snippet
    }

  13. bloggingfuture says:

    This is pretty valuable code for WordPress. Thanks!

  14. Alan Hughes says:

    So how would you apply this to a specific search bar? It doesn’t just apply the filter to every search bar on your site would it?

  15. scotte_sprott says:

    How can I limit search results to show just pages?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.