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:
- CPT books;
- CPT fruits;
- CPT cars
Each one of these custom post types has a custom taxonomy attached. So we have
- CPT books with taxonomy books-category
- CPT fruits with taxonomy fruits-category
- CPT cars with taxonomy cars-category
And each of these custom taxonomies have a couple of terms in them. So we have:
- CPT books with taxonomy books-category, with terms: drama, crime, adventure
- CPT fruits with taxonomy fruits-category with terms: apple, orange, banana
- 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:
<ul> <li><input type="text" value="" name="s" class="field s"></li> <li>Categories:</li> <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> </select> </li> <li><input type="submit" id="searchsubmit" value="Search" /></li> </ul>
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 :)