There are times when you make some specific category for just review or promotional stuff and would not want to get that included in your search results. This simple tutorial will help you to include only specified categories in your search results, so your users will have more accurate search results.

Procedure:

Note: Do take backup of the files before you try this.

01. Login to wordpress administration panel and go to Posts => Categories

02. Once there find the category you want to know the ID for and hover over it, you should see edit link. Right click on it and select Copy Link Location (in firefox) and then open a new tab and paste it there. You should see your category ID as tag_ID=xx. That xx is your category id.

03. Place this code in your functions.php file:

function searchcategory($query) {
if ($query->is_search) {
$query->set('cat','3,7');
}

return $query;
}

add_filter('pre_get_posts','searchcategory');


03. Simply change 3 and 7 to the categories you want to use for your search results, you can add as many as you want by separating them with commas (,).

This concludes this simple and useful tutorial. Hope you like it.