WordPress 3 custom search

Sometimes there is a need to limit WordPress search to one category. WordPress uses searchform.php template, if your theme doesn’t have this template – generic search form is pulled from WordPress core. In this case you need to create searchform.php file in your theme and insert following

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
<div class="customsearch>
     <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
           <div class="searchinput>
                  <input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
                      <?php if ( is_page('22') || in_category('19') || post_is_in_descendant_category( get_term_by( 'name','video','category' ) ) ) { ?>
                 <input type="hidden" name="cat" value="19" />
                     <?php } else {} ?>
                  <input type="submit" id="searchsubmit" value="Search" />
            </div><!--endsearchinput-->
     </form>
</div><!--end customsearch-->

In this case we have page (ID=22) which displays parent category “video” with ID 19, and we need to search only this category and all child categories. Just replace IDs.