How To Exclude Individual Posts From WordPress Search Results

Exclude Or Hide Posts From WordPress Search Results

WordPressThere are many reasons to exclude or remove certain WordPress posts from being searched on your WordPress blog or website. You may have off topic posts which don’t relate to your dedicated blog topic or you may have written embarrassing rants you do not want to include in your search results query.

Luckily, using the exclude-posts function will override the standard WordPress search function and will allow you to exclude certain posts from being displayed in your WordPress website’s search results.

Down Copy and paste the code below into your functions.php file.
Change “Post ID here” to your individual post IDs, separated by commas (ie: 301, 2234, 281).
[Normal_Box]

function exclude_posts($query) {
    if ($query->is_search) {
       $query->set('cat','Post ID here');
  }
  return $query;
}
add_filter('pre_get_posts','exclude_posts');

[/Normal_Box]
[Note]Locating WordPress Page And Post IDs[/Note]


Sean Doyle

http://Botcrawl.com

Sean Doyle is an engineer from Los Angeles, California. Sean's primary focuses include Internet Security, Web Spam, and Online Marketing.

Comments ( 2 )

  1. ReplyLeigh
    Same here, not having any luck this seems to break the search completely. Would be great to have a solution for this!
  2. ReplyAndyiBM
    Hi Sean - tried this, but all it succeeds in doing is breaking the search altogether, so no pages appear for any search term.