I have a search bar on the homepage of my theme. I want the user to be able to select from a drop down to search for everything, posts, or taxonomies. I looked at the wp_dropdown function, but its meant for searching under a single tag, and not within a taxonomie group.

now I just have a html dropdown next to the search form. how can I use the selection from the dropdown to modify the search?

Heres the code im working with

<form class="searchform" method="get" action="#">
            <legend class="hidden">Search form</legend>
            <div class="input-holder">
                <input class="search" name="s" type="text" value="Search Here"/>
            <select name="cat" id="cat" class="postform" style="display: none; ">
                <option value="0" selected="selected">Everything</option>
                <option class="level-0" value="1">People</option>
                <option class="level-0" value="2">Schools</option>
            <button value="submit" class="searchBtn"><span class="hidden">Submit</span></button>
            <!--END #searchform-->

I believe something like this would do the trick.


My code assumes the querystring value is the post_type, not an integer like your form field. Also, your form field is named 'cat', I think WordPress has that reserved, at least I remember having conflicts when I last used 'cat' in a querystring.


(Changed filter from 'posts_where' to 'pre_get_posts' after having done some tests)

In functions.php

 function my_filter( $query )
      if( is_search() )
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";

           if( $type )
                $query->set( 'post_type', $type );
 add_filter( 'pre_get_posts', 'my_filter' );
  • thanks this is perfect but how exactly do I implement it? – Steven Baltay Jan 26 '12 at 2:25
  • You're using WordPress' search, right? Or are you using something custom? If it's WordPress', all you need is to put the code above in your functions.php, and change your 'option' values to the post_type – Shane Jan 26 '12 at 13:49
  • Ok so this works great for searching post types, but how can I get this to work for taxonomy types? – Steven Baltay Feb 9 '12 at 17:17
  • @StevenBaltay Set the tax_query parameter : $query->set( 'tax_query', $tax_query ); – Shane Feb 9 '12 at 19:33
  • For information on the tax_query parameter : codex.wordpress.org/Class_Reference/… – Shane Feb 9 '12 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.