How to Display Specific Category Only In Search Result
3rd May 2011 | Posted by reza | No CommentsWhile customizing a WordPress site, sometimes We also need to configure, how our WordPress site display the search result. On our previous code snippet, I have showed how to exclude specific category from search result. This tutorial will be the reserve, display the posts from specific category only in search result. Maybe this is not a good idea, because the search result will be very limited, but lets just make this as a way to know more how actually WordPress works.
How To
Simply put the code below in the function.php of your theme.
function searchcategory($query) { if ($query->is_search) { $query->set('cat','2,5'); } return $query; } add_filter('pre_get_posts','searchcategory');
This code will make, only post from category with ID 2 and 5 display on the search result.
Hope this tutorial help.