WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
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

WPBeginner» Blog» Tutorials» How To Limit Search Results For Specific Post Types in WordPress

How To Limit Search Results For Specific Post Types in WordPress

Last updated on May 22nd, 2013 by Editorial Staff
32 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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.

32 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

26 Comments

Leave a Reply
  1. Ankit manandgar says:
    Feb 22, 2018 at 1:32 am

    how can i get search results according to post title only?

    Reply
  2. Anna says:
    Mar 11, 2017 at 4:53 am

    Thanks for this code – it worked, although you last updated in 2013! My theme uses also an Instant Search and I would like to limit the results there aswell. How could I do that?

    Reply
  3. Sparsh Goyal says:
    Jan 11, 2017 at 10:01 am

    My present theme shows post with few starting lines for searched term/word. I want to customise it to show that paragraph having searched term/word in post excerpt. In other words, I want to show related text in post excerpt not starting paragraph in search results. Can anyone help me to do this….

    Reply
  4. Azamat says:
    Nov 22, 2016 at 2:47 pm

    Hello,

    How can I limit search results for specific post types AND specific custom taxonomy terms?

    Reply
  5. Steven says:
    Jun 14, 2016 at 8:17 am

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

    Reply
  6. Greg says:
    Jan 12, 2016 at 4:26 pm

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

    Reply
    • Jonathan Joosten says:
      Feb 1, 2016 at 4:10 am

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

      Reply
      • Dan Sz. says:
        Apr 29, 2016 at 12:04 pm

        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.

        Reply
  7. Tadeu says:
    Dec 13, 2015 at 3:26 pm

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

    Reply
  8. Sandeep says:
    Sep 24, 2015 at 12:32 am

    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.

    Reply
  9. Jared says:
    Jun 10, 2014 at 7:17 pm

    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

    Reply
  10. Peter says:
    May 6, 2014 at 4:13 pm

    Thanks for the great tip!

    Reply
  11. WPBeginner Staff says:
    Apr 24, 2014 at 10:35 am

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

    Reply
  12. Nick says:
    Apr 23, 2014 at 10:57 am

    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?

    Reply
  13. emre says:
    Mar 31, 2014 at 12:55 pm

    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 :)

    Reply
  14. sam says:
    Mar 20, 2014 at 3:43 am

    How to limit by specific category? thanks

    Reply
    • WPBeginner Support says:
      Mar 20, 2014 at 1:41 pm

      Sam, please see our guide on how to add taxonomy search filter in WordPress.

      Reply
  15. Hasan Gad Allah says:
    Nov 7, 2013 at 5:39 am

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

    Reply
  16. Felix says:
    May 17, 2013 at 5:34 am

    There’s one problem with your snippet:

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


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

    Reply
  17. bloggingfuture says:
    Nov 16, 2011 at 7:32 pm

    This is pretty valuable code for WordPress. Thanks!

    Reply
  18. Alan Hughes says:
    Oct 5, 2011 at 10:56 am

    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?

    Reply
    • goJohnnyGo says:
      Jun 4, 2012 at 4:02 pm

      Did you ever get figure how to apply this to specific search bars (not universally)?

      Reply
      • Editorial Staff says:
        Jun 12, 2012 at 12:14 pm

        Yes we have an article here:

        https://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/

        Reply
  19. scotte_sprott says:
    Oct 1, 2011 at 8:57 pm

    How can I limit search results to show just pages?

    Reply
    • wpbeginner says:
      Oct 3, 2011 at 8:18 am

      @scotte_sprott In the array, just keep page and remove posts.

      Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WPForms Logo
WPForms
Drag & Drop WordPress Form Builder Plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
Connections Pro
Connections Pro Coupon
Get 15% OFF on Connections Pro WordPress directory plugin.
SeedProd Logo
SeedProd Coupon
Get 50% OFF SeedProd Coming Soon Page plugin for WordPress.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.