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

How to Exclude Pages from WordPress Search Results

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Exclude Pages from WordPress Search Results

By default, WordPress Search feature displays published posts and published pages in search results. Often when users are looking for something in a blog, it is most likely a post rather than a page. In this article, we will show you how to make your search more relevant and less crowded by excluding pages from WordPress search results.

First open your theme’s functions.php file and paste this code:

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    return $query;


Explanation: This code just searches for posts through setting the post_type. You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search result.

Source: Bavotasan

If you want to exclude individual pages from WordPress search results, then use the plugin: Simply Exclude.

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 »


  1. Joni says:

    Thanks! :)!

  2. Laura Rafferty says:

    Wondering if there’s a way to target different search forms with this technique. I’d like the main website search form to target pages only; and the blog search form to target posts only.

  3. Dmytro Shylov says:

    This one works better.
    The fix does not affect custom post types and does not break the default wp’s flow.

    add_filter(‘register_post_type_args’, function($args, $post_type) {
    if (!is_admin() && $post_type == ‘page’) {
    $args[‘exclude_from_search’] = true;
    return $args;
    }, 10, 2);

  4. Val says:

    I have some pages that are password protected (via main WP option to password protect a page) but they are still showing up in WP search with the excerpts. My understanding is that by default, the excerpts should not appear, you should see “There is no excerpt because this is a protected post.” I can use the Simply Exclude plugin but I’m wondering why the excerpts are showing up in the first place and I’d rather not add another plugin if I don’t have to.

  5. deny jackrose says:

    Thank you very much sir.. this post really help me.. you rock man .. !!

  6. Victor says:

    Thank you very much. You guys did the trick. I was looking for this and couldn’t find it anywhere else.
    Nice job, guys.

  7. Sony says:

    Add this to your theme’s functions.php for displaying only products using wordpress search.

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set(‘post_type’, ‘product’);
    return $query;

  8. Kelly says:

    I really hope you can help me out; does this works for showing ‘ products’ only in search results? I need to exclude ALL ‘ pages’ since I only want to show my products in store.

    • WPBeginner Support says:

      It would be not the best option to do that. You would probably want to use a search plugin like SearchWP.

      • Kelly says:

        Actually I found a simple sollution elsewhere. Just put this code in your functions.php (of child theme):

        // Set search results to display only ‘product’ post type results
        if ( !is_admin() ) {
        function searchfilter($query) {
        if ($query->is_search && !is_admin() ) {
        return $query;

  9. Noah says:

    When someone just hits SEARCH with the criteria being ‘blank’, ALL pages show up in the results. How can I have NO results show in this situation?

    • Jornes says:

      Noah, if you are saying about search result, you can validate the search form using javascript. So that people are forced to key in the words before do the searches.

  10. Samsir Alam says:

    Thanks bro.

  11. Dirk says:

    Thanks for the Code!

    One remark: I think you should make it more clear in the article, that this snippet is responsible for all searches especially in the admin area.
    So also the tiny seachfields in pages, the search for a page to link to in the editor and the dropdown in the media part won’t work.

    I just realized this some weeks later, when i first wondered why the enhance media library plugin (which adds categories to media files) won’t work.
    I stumbled upon your note to add “if(!is_admin()) ” in the comments just too late, so maybe better put it directly in the article.

    Nevertheless: Every small problem and bug just enhances my knowledge of WordPress :-D

  12. Sonia says:

    How would I add code to the following so that it would exclude an optin-form from displaying on pages? I only want it to display on posts. Thanks.

  13. WPBeginner Staff says:

    Glad to hear. You are welcome.

  14. WPBeginner Staff says:

    Can you tell us what error you saw when you were unable to access your site? This will help us figure out exactly what went wrong.

    Most of the time users forget to close an open tag. Missing characters in the PHP code can also break the script resulting in an error.

    Meanwhile, please checkout our “guide on pasting snippets in WordPress.

    • Debbie Andres says:

      I’ve since fixed the problem and you’re exactly right. In my ‘newness’ to all this I just entered the code literally at the end of the file — after the ‘?>’ –instead of before and that’s what ‘broke’ things! Once I entered it before the closing tags it all worked out. Thanks again for your help!

  15. Debbie Andres says:

    I’m really new to all of this so I have to ask: does it matter where in my theme’s functions.php file that I paste this code? In the past when I’ve had to enter code into this file, it’s had to go at a certain spot. Thanks!

    • WPBeginner Staff says:

      You can place the code any where but to make sure that you do not break anything it is recommended to add your code at the bottom of your functions.php file.

      • Debbie Andres says:

        Thank you so much!

        • Debbie Andres says:

          Hi again,
          So I entered the code at the end of my functions.php file and something went wrong because after that I was unable to log into my WP dashboard.
          So I went into the File Manager of my hosting acct and removed the code I’d entered but even after that I still couldn’t log into my dashboard.
          The only way I was able to get back into my dashboard was to replace the existing functions.php file with the original one.
          Do you have any idea why this would have happened?

  16. Dimitra says:

    Thank you so much for the article and some comments with codes that helped me a lot!

  17. Bijay Pakhrin says:

    Worked perfectly, Thanks alot :)

  18. redimin says:

    Work Fine !

  19. Bruno says:

    Thanks bro! Your code helped me a lot.

  20. Henry says:

    This worked much better than passing a hidden field (which is what I’ve been doing).

  21. Laura says:

    This worked great, thanks! How can I include WordPress Native BBPress forum topics in the search results?



  22. Jornes says:

    Hi! I would like to ask how do i exclude the specified pages i wanted to excluded from searches? And how do i know what is the ID of the page? Can someone tell a solution? I wanted to excluded some pages which are useless to visitors.

    Thank you!

    • EffectiveLab says:

      function jp_search_filter( $query ) {
      if ( $query->is_search && $query->is_main_query() ) {
      $query->set( ‘post__not_in’, array( 10,11,20,105 ) );
      add_filter( ‘pre_get_posts’, ‘jp_search_filter’ );

  23. hamza says:

    works for me. thank you!

  24. Stephen says:

    So to exclude posts, do I add:

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set(‘post_type’, ‘pages’);
    return $query;


  25. Spyridon says:

    You are a real life saver. I tried codes from WordPress forum but didn’t work out. Yours is working for me! :D

  26. Gormley says:

    Code works on site search but it also stops “search media” within the media admin section from working as well which is a big issue.

  27. Timothy says:

    So how would I go about excluding a specific page, meaning page ID?

  28. Jornes says:

    Hi! Your code is helpful. But my excerpt code that limit the words from showing from my search, index and categories page not working. I have limited it as 20 words, but now the limitation is less than that. How to fix it?


  29. Waqas Khan says:

    Can you please tell me how to exclude only some specific pages from the search? For example I wan to exclude the page “bio” from the search but not the resume, awards pages.
    I want the posts and pages in search except the bio page.

  30. StylishandTrendy says:

    I was able to exclude categories too based on your search filter code

    function SearchFilter($query){

    if ($query->is_search)

    { $query->set(‘cat’,’8,15′);


    return $query;



  31. luckyankit says:

    Well just wanted to say thank you for this… wonderful article

  32. gormanIT says:

    Can you alter this function to exclude specific pages/posts from search? Ive been searching for this functionality and havent found anything.

  33. gormanIT says:
  34. David Allred says:

    Perfect! I was expecting it to be a bit more complex than that. Thanks a ton!

  35. Michael Bailey says:

    Thank you so much. This was super helpful, fixing my problem with the speed of a cut and a paste (and a test)..

    Success in all your ways!


  36. Katie says:

    Well, that’s almost perfect. I searched and found that many site’s offering us to hardcode the category ids to search id but this is cool. One question, i have to do the pagination myself so when i add this filter i won’t get offset issues .. right?

  37. Art Armstrong says:

    You need to modify this sentence to say “page” instead of “pages”:

    You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search result.

  38. Mohsen says:

    may I put your tutorials in my blog with a link to my blog is in Farsi and I think many persian users need these tips.
    thanks a lot

    • Editorial Staff says:

      No you may not translate any of our tutorials entirely. You may write a summary and then link to our site.

  39. Rilwis says:

    This hack excludes not only pages from search results, but also other custom post types. So if we have many custom post types in our WP site, we can put them in the statement like this:

    $query->set(‘post_type’, array(‘post’, ‘custom-post-type’));

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.