How to: Exclude posts or pages from search results ?

Would you like to be able to control which post or pages must be exclued from searches on your WordPress blog? If yes, this recipe will probably help you a lot. Just read on.

Copy and pase this code in your themes function.php file

// search filter
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(1805,1130) ); // id of page or post
}
return $query;
}
add_filter( 'pre_get_posts', 'fb_search_filter' );

Save File.
Enjoy code!

Leave a Reply

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