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:

[code]
function SearchFilter($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}
add_filter(‘pre_get_posts’,’SearchFilter’);
[/code]

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:

[code]

[/code]

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

Related Terms:
Posted on 20th June 2013 by in Wordpress
Web Agency Portsmouth
where to go from here? get in touch OR

give us a call on 02392 123 538

Let's be friends! More...
Facebook Twitter Google+
Like & Follow Facebook Twitter Google+
Share the love, share this page! Facebook Twitter Google Digg Reddit LinkedIn Pinterest Email
Close [X]
Web Design South Coast
Areas We Cover

Here are some of the local areas we provide with web design and digital marketing services. These are just some examples, we work with clients all over the UK and across the globe.

Our Services
Close [X]
The Web Taylor
Harcourt Close Waterlooville, Hampshire PO8 8JL
02392 123358