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

Sean Doyle is a tech author and engineer with over 20 years of experience in cybersecurity, privacy, malware, Google Analytics, online marketing, and other topics. Sean's content has been featured in numerous publications.

2 Responses

  1. Leigh says:

    Same here, not having any luck this seems to break the search completely. Would be great to have a solution for this!

  2. AndyiBM says:

    Hi Sean – tried this, but all it succeeds in doing is breaking the search altogether, so no pages appear for any search term.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.