If you’ve worked with WordPress, you’ll know that the search functionality isn’t the best and probably deserves a good look at to bring it up to speed with the amazing system that WordPress is! Anyway, today Brendon and I were finishing up on a project we’ve been working on and one requirement was to get the site search working, but to only return results from one category and we found an awesome way to do it:
You standard search form code looks like this in WordPress:
<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div><input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />
<input type=”submit” id=”searchsubmit” value=”Search” />
</div>
</form>
Now, in order to return results from just one category, there’s a quick fix by adding just 1 line of code, here’s the reworked code:
<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div><input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />
<input type=”hidden” name=”cat” value=”4″ />
<input type=”submit” id=”searchsubmit” value=”Search” />
</div>
</form>
What we are doing here is simply passing a hidden variable along with the search string so that your search would take the format of this:
http://www.imod.co.za/?s=Keyword&cat=4
What this does is perform a search on the search term, “Keyword”, within category 4. Naturally you can change category 4 to whichever category you wish.
Pretty nifty innet?





