Exclude Pages From Search Results

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:




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:


//Search only pages
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!

9 thoughts on “Exclude Pages From Search Results

  1. 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!

  2. This causes a huge problem visually with pagination though. Some page results have 6 items listed, some have 10. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>