elegant themes

How to Hack the WordPress Search Function: Search Categories and Child Categories

| July 21, 2009 | 22 Comments

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.

Tags: , , , , , , ,

Category: Code Snippets, WordPress as CMS

About Rebecca Markowitz: Rebecca Markowitz has built over 50 WordPress sites for clients as Web Project Manager and WordPress specialist at illuminea. illuminea is a Jerusalem-based boutique web agency. WPGarage shows my dedicated relationship with WordPress over the years - full of love, laughs, tears, growth and strong drinks. L'chaim! View author profile.


Comments (22)

Trackback URL | Comments RSS Feed

  1. […] 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 […]

  2. […] 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 […]

  3. Michel says:

    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:

    &lt;?php $categories = get_categories(’child_of=5?);&gt;
    Please advise,
    Many thanks,
    Michel

  4. Alek says:

    I LOVE YOU MAN!… works perfectly.. unlike most of those other tutorials out there..

  5. Lane says:

    Thanks for this.  You are a ninja.  How would you code a search that searches all categories EXCLUDING one?

  6. Nicolas says:

    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? />

  7. LDQ says:

    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 :)

  8. Matt says:

    Doesn’t seem to be working with Search Unleashed plugin.  Thats too bad. Otherwise, good addition.

  9. phil says:

    Hum, I might be stupid or something but how do you implement this on a page?

  10. Jason says:

    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=

  11. Kevin says:

    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?

  12. […] 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 […]

  13. 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
     

  14. activo says:

    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>

    • activo says:

      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>

  15. […] 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 […]

  16. Amy Beeton says:

    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. :(

  17. Gerald says:

    How do I create a custom search form to retrieve results from a parent page to the children pages within that parent page?

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Code Snippets, WordPress as CMS (23 of 54 articles)