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 article will probably help you a lot.

To achieve this, just simply paste the following code on the functions.php file from your theme.

In this example, your wordpress posts with IDs 1 and 10 will be excluded from your wordpress blog’s search results:

// Exclude these page ids from search results
function my_search_filter($wp_query) {
   if ($wp_query->is_search) {
      $wp_query->set('post__not_in', array(2028,1977,185,181) );
   }
   return $wp_query;
}
add_filter('pre_get_posts','my_search_filter');

Revisions

There are no revisions for this post.

Tags: ,

No comments yet.

Leave a Reply