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 is a distinguished tech author and entrepreneur with over 20 years of extensive experience in cybersecurity, privacy, malware, Google Analytics, online marketing, and various other tech domains. His expertise and contributions to the industry have been recognized in numerous esteemed publications. Sean is widely acclaimed for his sharp intellect and innovative insights, solidifying his reputation as a leading figure in the tech community. His work not only advances the field but also helps businesses and individuals navigate the complexities of the digital world.

More Reading

Post navigation

2 Comments

Leave a Reply

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