How to Hack the WordPress Search Function: Search Categories and Child Categories
Recently, we had a client who wanted a special recipe search. There were 2 ways to extend the search functionality – manually or dynamically.
1. Search One or More Categories Manually
If we want to search one category, we could have used this solution which lets you manually add category numbers to the search function:
[Replace value="5" with your category number.]
<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=”5″ />
<input type=”submit” id=”searchsubmit” value=”Search” />
</div>
</form>
2. Search Subcategories Dynamically
However, we wanted to dynamically search only the subcategories of the Recipe Category. The reason being that if you’re already in the Recipes section of the site, seeing a recipe labeled as Recipes is not helpful. However, seeing a recipe labeled as Dessert (mmm..) and Breakfast is helpful. So, each recipe that the client adds will be in a subcategory of recipes (ex. Dessert), but they won’t have to check off the checkbox next to the Recipes category itself.
Anyway, I’ve been looking for this solution for a long time and now, thanks to Ilan Cohen, I present it to you.
[Replace the number 5 with the parent category.]
<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”search”>
<input type=”text” value=”Search… ” onclick=”this.value=”;” name=”s” id=”s” />
<?php $categories = get_categories(‘child_of=5′);
$catlist = ”;
foreach ($categories as $cat) {
$catlist.= $cat->cat_ID.’,';
}
$catlist.’5′;
?>
<input type=”hidden” name=”cat” value=”<?php echo “$catlist”?>” />
<input name=”" type=”image” src=”<?php bloginfo(‘stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” class=”btn” />
</div><!–/search –>
</form>
The code searches through all the child categories of Category 5 (Recipes). Then, you’ll see we added an additional “5″ in this line: $catlist.’5′; just in case the client puts a recipe in the Recipes category, and not one of its subcategories.
Category: Code Snippets, WordPress as CMS










[...] more: How to Hack the WordPress Search Function: Search Categories and … Share this on del.icio.usDigg this!Buzz up!Stumble upon something good? Share it on [...]
[...] the previous post, we discussed how to hack the search function in WordPress to have an additional search form that would search subcategories. Now that you have [...]
Hi,
Thanks for Sharing,
I tried the multiple categories search and all I did was copy/paste the code above in my index page replacing “5″ with cat”3″.
I had an error message saying there was something wrong on line:
<?php $categories = get_categories(’child_of=5?);>
Please advise,
Many thanks,
Michel
I LOVE YOU MAN!… works perfectly.. unlike most of those other tutorials out there..
Thanks for this. You are a ninja. How would you code a search that searches all categories EXCLUDING one?
To exlcude a category you must add – (minus) in front of the cat id.
In the first example it would be <input type=”hidden” name=”cat” value=”-5? />
You are my master:)
I’m working on wordpress 2.92 + plugin Exec-PHP and I past in to html/text widget you’r 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=”28,7″/>
<input type=”submit” id=”searchsubmit” value=”Search…” /></div>
</form>
I separate categories by comma and it’s working
Doesn’t seem to be working with Search Unleashed plugin. Thats too bad. Otherwise, good addition.
Hum, I might be stupid or something but how do you implement this on a page?
I know its a little late but you can use this code with Search unleashed, all you need to do is change the word ‘cat’ to ‘post_category’ since search unleashed describes categories as ‘post_categories’ and wordpress search uses ‘cat’.
So any return when searching category 5 would be
/?post_category=5&s=
instead of
/s=
[...] Source: wpgarage.com → [...]
Maybe someone can help me with this one. I have a product list on my site being pulled from an additional table in mysql. I would like to include these products in the search. Is this possible?
[...] Source: wpgarage.com ? [...]
[...] always the best, so it did occur to me to use Google Custom Search instead, but having discovered ways to hack the standard search function, the free option seemed the best way to go – at least initially. As with any web project, things [...]
Hi you can use this code with Search unleashed
http://www.ramoservice.com/thesis-themes/add-search-box-in-thesis-theme/
i am in search of multiple category or tags search plugin. I have tried a plugin that is multi category search widget but it is not helping me as i need. i have a <a href=”http://www.best-tv-price-review-india.com” target=”_blank”>television</a><br />site and i am looking for a plugin that can help my users to select television by category
great snippet man!
i was trying to make two checkboxes to give the possibility to the user to decide in wich category to search.
i have only two category in my website, but it doesn t work..seems that that the form add to the url the category filter only when it is passed by an “input=”hidden”..is it possible?
this is the code i tried..:
<code>
<form method=”get” id=”searchform” action=”<?php bloginfo(‘url’); ?>/”>
<input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />
<input type=”checkbox” name=”cat” value=”3″ />News
<input type=”checbox” name”cat” value”4″/>Annunci
<input type=”submit” id=”searchsubmit” class=”submit” value=”Cerca” />
</form>
thanks in advance!
<code>
sorry i forgot two equal
but itdoesn t work anyway
here’s code i tried
<form method=”get” id=”searchform” action=”<?php bloginfo(‘url’); ?>/”>
<input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />
<input type=”checkbox” name=”cat” value=”3″ />News dal web
<input type=”checbox” name=”cat” value=”4″/>Annunci pubblicati
<input type=”submit” id=”searchsubmit” class=”submit” value=”Cerca” />
</form>
i’m sorry it worked
thank you very much for your enlightment !
[...] value=”Go” class=”btn” /> </div><!–/search –> </form>Source: wpgarage.com →Search a Specific Post TypeWordPress gives you the ability to search specific post types by using [...]
I really can’t get this to work. I’m trying to Search Subcategories Dynamically but the encoding is wrong on the snippet, i’ve changed it all so it should work but still nothing.
How do I create a custom search form to retrieve results from a parent page to the children pages within that parent page?