Excluding certain pages or posts in built-in WordPress search results

Last updated on December 31, 2011. Tags:

Note: This is an old and outdated method of excluding pages and posts in WordPress search. This method is ugly, more complicated and does not display the same number of posts/pages per search result page. For a more effective and elegant method of excluding static pages and posts from WordPress search results, please click here.

One of the reasons why I switched from static HTML to a WordPress-powered website is that it comes-up with a search function, something that will come very handy by the time I already wrote so many articles in this blog.

However, one of the problems that I encountered is that WordPress, by default, returns all posts and pages that matched the query. Once possible scenario is that a visitor is looking for a way to create a "thank you" page, and one of the results is the "thank you" page itself, which should not be accessible unless the visitor left a message or did something you want them to do in your website.

I read somewhere that in earlier versions of WordPress, the search function returns only the posts. Then, some users suggested that the page should also be included. I started using WordPress 2.3 and I am currently using 2.7. In both versions, the default is that all matching posts and pages are listed in search results.

The code responsible for controlling what appears in the search results is the code shown below. You can found it in search.php1 of your WordPress theme.

<?php while (have_posts()) : the_post(); ?>

Exclude all pages from search results

For most bloggers, they would like to restrict the search function into showing only the posts and none of the pages. To prevent all pages from appearing in the search results, replace the default code with the following:

<?php while (have_posts()) : the_post(); if($post->post_type == 'page') continue; ?>

Exclude specific pages or posts from search results

In some cases, we want to restrict only one, two or few pages or posts from appearing in the search results. Some of the entries that you would not want to appear in the search results are the contact form page and the "thank you" page. To prevent one particular page/posts from appearing in the search results, replace the code above with the one below:

<?php while (have_posts()) : the_post(); if($post->ID == '17') continue; ?>

In the code above, the page that we do not want to appear in the search results has post ID of 17; you can replace this number with whatever number assigned to the page that you do not want to appear in search results. In case ID=17 is assigned to a post and not to a page, the code will still work, but this time, preventing that certain post with ID of 17 from appearing in search results.

To prevent two or more pages or posts from appearing in the search results, we modify the code a little so that it can accomodate more than one post ID.

<?php while (have_posts()) : the_post(); if($post->ID == '17' || $post->ID == '19'|| $post->ID == '20') continue; ?>

In the code above, the WordPress' search function will never list the pages or posts with IDs of 17, 19 and 20. The symbol || is the OR argument, which you can repeat as many times as you need.

Exclude all pages and a few specific posts in search results

To prevent all pages and some posts from appearing in search results, we will combine the PHP code immediately above with the two other codes that comes prior to it to form the code shown below.

<?php while (have_posts()) : the_post(); if($post->post_type == 'page' || $post->ID == '26'|| $post->ID == '30') continue; ?>

The code above prevents all pages as well as posts with post IDs of 26 and 30 from appearing in search results.

Footnote:

  1. This technique will work only if your WordPress theme has search.php. Apparently, not all WordPress themes have this PHP file; one example is the "Classic WordPress Theme". The index.php contains the same default codes but I haven't experimented on it yet.

Posted by Greten on January 10, 2009 under WordPress

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by Tracey Grady on 06.30.09 11:02 am

    I like the idea of this. However, when I tried to implement it, I got parse errors.

  2. Posted by Greten on 07.08.09 6:44 pm

    Hi Tracey

    Can I see the exact codes that you used?

    It’s possible that along the way, one of the quotes changed to curly quotes. Php codes don’t work with curly quotes.

  3. Posted by Karl Macklin on 07.10.09 8:15 pm

    Pretty good solution, but the problem arises when your search results are ONLY pages.

    What happens then is that your default “no search results found” message wont get displayed, since the loop runs (although continues out immediately) rather than failing the conditions, which is required to get the prefered “no results” message.

  4. Posted by Greten on 07.20.09 3:00 am

    Hi Karl,

    I think this is an effect of how the loop works, similar to the way when the search results is supposed to display 10 results per page, but one of the results was blocked from appearing using this technique. The search results will display only 9 instead of getting the first from the next page.

    I first thought that’s ok. I never foresaw this possibility. Now I need to do some revisions of the codes above. I’ll leave this post as is and write a new post about your concern.

  5. Posted by Greten on 07.20.09 6:42 am

    Karl,

    Thanks for noticing. Now, I just found a new way to exclude pages and certain posts to WordPress. I’ll write it now and will be published within this week.

  6. Posted by Hi Karl on 06.11.10 10:08 am

    Thanks for sharing this.. It helps me control related posts to my page :) .. Am a newbie in wordpress stuffs this bit worth a big thing for me..

Post Comments





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.