Search Specific Category in WordPress

There are some times when you want to limit your search to a particular category, or perhaps multiple categories. This is relatively simple to do in WordPress by adding a hidden field to your search code. (See example below)

PHP:
  1. <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
  2. <div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
  3. <input type="hidden" name="cat" value="22" />
  4. <input type="submit" id="searchsubmit" value="Search" />
  5. </div>
  6. </form>

You can see I added my hidden input field on the third line. When I add this in, it then adds onto the query used to search. Your search will go from something like http://www.sandboxdev.com/?s=WordPress to http://www.sandboxdev.com/?s=WordPress&cat=22 and will only return posts in the category ID you choose.