Refine search by category

DescriptionWordpress snippet by - 11/22/10

Use this snippet to add functionality to the default wordpress search form. First, edit your search.php file and add the first snippet above the wordpress loop. Then copy and paste the search form within your template files wherever you want to display the form.

Tags ,

//Add this to the search.php file, just above the wordpress loop
<?php $search_category = $_GET['category']; ?>
<?php query_posts('&cat=' . $search_category); ?>

//Add this wherever you want to display the search form

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/" class="search-form">
<input type="text" value="Search" name="s" id="s" class="field" />
<select name="category">
<?php
$categories = get_categories();
foreach ($categories as $cat){ ?>
<option value="<?php echo $cat->cat_ID; ?>" <?php if($cat->cat_ID == 3) echo 'selected="selected"'; ?>><?php echo $cat->cat_name; ?></option>
<?php
}
?>
</select>
<input class="submitsearch button" type="submit" name="submit" value="Search" />
</form>
  • Share
Authored by: Adam J Nowak
http://hyperspatial.com

Comments and Feedback