Exclude pages from search results in WordPress

Exclude pages from search results in WordPress

My blog has a Mac section. This Mac section is a page with one article which contains all the software. When someone uses the search functionality from this blog, and searches for VPN the whole Mac section appears in the search results which is pretty annoying. This website explains how to exclude all pages from the search results.

  1. Open your WordPress admin
  2. Goto Appearance
  3. Click on Editor in the Appearance menu
  4. On the right side of the pages click on theme functions
  5. Add the following code to this section
function vibeExcludePages($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','vibeExcludePages');

Leave a Reply

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