I am developing a multiple search filters function and I need to filter the search results page.

I have 3 custom post types, each one with its custom taxonomy. So I have the custom post types:

  1. CPT books;
  2. CPT fruits;
  3. CPT cars

Each one of these custom post types has a custom taxonomy attached. So we have

  1. CPT books with taxonomy books-category
  2. CPT fruits with taxonomy fruits-category
  3. CPT cars with taxonomy cars-category

And each of these custom taxonomies have a couple of terms in them. So we have:

  1. CPT books with taxonomy books-category, with terms: drama, crime, adventure
  2. CPT fruits with taxonomy fruits-category with terms: apple, orange, banana
  3. CPT cars with taxonomy cars-category with terms: mercedes, ford

In the searchform.php I have the search input AND a function that gets ALL the terms from ALL my custom taxonomies and puts them into a drop down.

So the HTML looks like this:

    <li><input type="text" value="" name="s" class="field s"></li>
        <select class="postform" id="cat" name="cat">
            <option selected="selected" value="0">All categories</option>
            <option value="99">Adventure</option>
            <option value="41">Apples</option>
            <option value="23">Banana</option>
            <option value="31">Crime</option>
            <option value="19">Drama</option>
            <option value="103">Ford</option>
            <option value="24">Mercedes</option>
            <option value="67">Orange</option>
    <li><input type="submit" id="searchsubmit" value="Search" /></li>

When a user leaves the search input empty, but selects a taxonomy term in that drop-down, I need the search page to filter my search results so that it displays only custom post types filed under that taxonomy term.

The big mystery here how can I find a way to get the TAXONOMY NAME to use in the WP_Query class, giving the fact that I have the TERM NAME and the TERM ID (the "value" field from my drop-down form). I could do this easy in taxonomy.php, but here I am in search.php!

Any help would be greatly appreciated :)

Best, Gabriela

2 Answers 2


How about using the get_term function? If you look at the code the sql it uses is as follows:

$_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) );

which should be exactly what you need!


in the options value you could use something like this

<option value="tax_slug_a[ 103 ]">Ford</option>
<option value="tax_slug_a[ 24 ]"> Mercedes</option>
<option value="tax_slug_b[ 67 ]"> Orange</option>

the resulting form submission would give you an array with both chosen the tax slug info and the term id.

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.