1

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="#">
        <fieldset>
            <legend class="hidden">Search form</legend>
            <div class="input-holder">
                <input class="search" name="s" type="text" value="Search Here"/>
            </div>
            <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>
            </select>
            <button value="submit" class="searchBtn"><span class="hidden">Submit</span></button>
            <!--END #searchform-->
        </fieldset>
    </form>

1 Answer 1

1

I believe something like this would do the trick.

EDIT

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.

EDIT 2

(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' );
8
  • thanks this is perfect but how exactly do I implement it? Commented Jan 26, 2012 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
    Commented Jan 26, 2012 at 13:49
  • Ok so this works great for searching post types, but how can I get this to work for taxonomy types? Commented Feb 9, 2012 at 17:17
  • @StevenBaltay Set the tax_query parameter : $query->set( 'tax_query', $tax_query );
    – Shane
    Commented Feb 9, 2012 at 19:33
  • For information on the tax_query parameter : codex.wordpress.org/Class_Reference/…
    – Shane
    Commented Feb 9, 2012 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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