• Menu
  • Call: 03333 110 233

Social

Close Menu

WordPress: How To Exclude Pages From Search Results

When performing a search on a WordPress site, by default the results will be posts and pages. Usually when people are searching on a blog, they are most likely to be looking for a post rather than a page. There are two ways to exclude pages from showing up in search results.

The first method will completely block pages from search results. Simply copy and paste the below code into your functions.php file:

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

The second method gives the user the choice of searching for posts, pages or both. This is done by changing the way your search form works. The following code should replace the code of your current search form:


<form id="searchform" action="/" method="get"><input type="text" name="s" placeholder="Search..." /><select name="post_type"><option>Posts</option><option>Pages</option><option>Both</option></select><button>Search</button>

</form>

Explanation: So, by adding the select with the name of “post_type” it adds a string to the search URL telling WordPress to only search for posts or pages. For example, the following URL will show only posts: http://cazue.com/?s=exclude+pages+search&post_type=post

Last updated by at .

Related Terms:
Posted on 20th June 2013 by in Wordpress
About

Hi, I'm Stuart. Co-Founder of Cazue and owner of The Web Taylor, I have a passion for anything techy - whether it be web design, coding, SEO or social media. Wordpress is my favourite open source software of all time and I love writing about it! Find me on

has published 92 posts for Cazue.
Random ArticleClick Here NOW!
Free Website & SEO Report/Review

Popular Articles

Search Articles

Popular Tags