Search from Specific Category Only in WordPress

3d Animation

Using the standard WordPress Search functionality to search from a specific category / archive only. Thanks to the guys at these simple tips mean you can laser focus your WordPress searches on specific categories!

Add this code to your searchform.php file to only search from a specific category id.

line of code to add to only show search results from category id - 6

    <input type="hidden" name="cat" id="cat" value="6" />

full code here

    <form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="hidden" name="cat" id="cat" value="6" />
    <input type="text" size="16" name="s" value="Search"  />
    <input type="submit" value="Go" />

You can also exclude a specific category from a Search by adding this code, notice adding a minus value next to the category id (in this case 6) to exclude it from the search.

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( "s=$s&paged=$paged&cat=-6" );