Exclude Pages From Search Results
Category : Wordpress
Many Wordpress bloggers want to exclude pages from their search results and only search through posts.
True, there are plugins available that will help you accomplish this, but I always try to avoid installing slow, bulky plugins when I don’t have to. Using this method, you only need to add 1 line of code to your search.php file.
Search Only Posts
To exclude all pages from your search results and only search posts, open up your search.php file located in your theme’s directory.
Towards the top of the page, you will see:
Just below that line, add the following:
So now, it will look like this:
<?php if (is_type_page()) continue; ?>
Using a simple PHP if statement, we only search through posts.
The last step is to open the file wp-includes/functions.php and scroll down to the very bottom just before you see:
and enter the following:
function is_type_page() {
global $post;
if ($post->post_type == ‘page’) {
return true;
} else {
return false;
}}
That’s all there is to it! Now any searches performed on your blog will show only post results and exclude all the pages!
Popularity: 45% [?]

















Free Online Tutorials




Nice tips there. I would say why not install Platinum SEO pack, it has option of nofollow and noindex of a post.
Well, like I said, I always try to avoid installing bulky plugins when possible, when all I have to do is edit a little code.
Not only that, but then I would have to remember to noindex every post I write. This way, I edit some code one time and forget about it.
Thanks for the comment!
You left something out of your description – you also need to add the is_type_page function to your functions.php as described in the Wordpress support forum.
@John Faulds:
Yes, you’re exactly right. Thanks for pointing that out. I’ve updated the post to include the proper information.
Put the function in the functions.php file of your theme, NOT in wp-includes/functions.php!!
@ Eric:
It works either way.
Nope. Once you upgrade WP, your new function is gone!
This causes a huge problem visually with pagination though. Some page results have 6 items listed, some have 10. Any ideas?
Hi folks, there’s a simpler solution that solves the problem at the root – and should mean you don’t have the situation where the pagination is ruined by it finding results, but none in your posts…
http://wordpress.org/support/topic/197339