By default, WordPress search will display relevant results from entire post content on your website. Do you want to stop specific posts from appearing in WordPress search results? This can be easily implemented using piece of custom code.

Code to exclude category in WordPress search results

1. To get started, assign specific category to all posts that you want to exclude (or hide) from appearing in WordPress search results listing.

2. Then add following code to functions.php file.


is_search && !is_admin() )
$query->set( 'cat','-7' );

return $query;

}

In above code, change 7 with ID of the category that you want to exclude in WordPress search results listing. Quick and simple, isn’t it? [Credit Jared Atchison]

Davinder Singh Kainth

A digital creator with 15+ years of experience in Website Design, Development, SEO, and Content Creation to Podcasting at SmartWebCreators.com with the motto of "Be Smart, Keep Creating". A coach, consultant, and your dear geek friend ❖

Leave a Comment