WP Tip - Exclude pages from search results

December 2nd, 2008 | Categories: WP Tips-Tricks | 132 views

If you intend to exclude pages from search results, you can use the Simply-Exclude plugin. However, another solution that works pretty well is to add a filter in functions.php to only include posts categories instead of excluding pages from search results:

1
2
3
4
5
6
7
function mySearchPostsFilter($query) {
if ($query->is_search()) {
$query->set('cat','12,14,16,17,18'); // your category IDs
}
return $query;
}
add_filter('pre_get_posts','mySearchPostsFilter');

Just replace the posts category IDs (’12,14,16,17,18′) by your owns and it should work. That’s all!

Sign up for News and Updates!
digg delicious stumbleupon technorati Google live facebook Sphinn Mixx newsvine reddit yahoomyweb
No comments yet.
Off Auto TELEX VNI VIQR VIQR*
Quote seleted text