Last updated on December 15, 2009. Tags: WordPress search
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(); ?>
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; ?>
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.
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.
Posted by Greten on January 10, 2009 under WordPress tweaks
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.
* Required. Your email will never be displayed in public.
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.
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.
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.
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.
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.
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..