Exclude Page From WordPress Search

WordPress, by default search for post and pages. How ever you can add a filter to exclude page or post to make you search result effective.


Open your functions.php from your active theme and use the following Code.


function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post'); // Displays only post. Change it to 'pages' for pages.
}
return $query;
}

add_filter('pre_get_posts','SearchFilter');


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

COMMENTS

No Comments

There are no comments posted yet. Be the first one!

Leave a Reply