I have three custom taxonomies, Genre, Type and Ayear, with multiple terms in each. How can I create a multiple Drop-Down that lists all the terms for each custom taxonomy that a user can filter when they hit a submit button?
I found this code snippet and it works partially, but when I hit submit, it returns the terms id’s rather than the slugs. So I get www.site.com/?genre=6&type=5&ayear=3
How can I get it to return, www.site.com/?gene=action&type=new&ayear=2009
function my_dropdown($name, $taxonomy = 'category')
{
$defaults = array(
'taxonomy' => $taxonomy,
'id' => $name,
'name' => $name,
'show_option_none' => ' - Select - ',
'selected' => get_query_var($name)
);
wp_dropdown_categories($defaults);
}
add_action('pre_get_posts', 'my_customsearch');
function my_customsearch($query)
{
$tax_query = array();
$genre = (int) get_query_var('genre');
$type = (int) get_query_var('type');
$ayear = (int) get_query_var('ayear');
// first dropdown
if (!empty($genre) && $genre > 0) {
$tax_query[] = array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => $genre
);
}
// second dropdown
if (!empty($type) && $type > 0) {
$tax_query[] = array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $type
);
}
// third dropdown
if (!empty($ayear) && $ayear > 0) {
$tax_query[] = array(
'taxonomy' => 'ayear',
'field' => 'slug',
'terms' => $ayear
);
}
if (sizeof($tax_query) > 0) {
$tax_query['relation'] = 'AND';
$query->set('tax_query', $tax_query);
}
}
// add my custom query vars
add_filter('query_vars', 'mycustom_query_vars');
function mycustom_query_vars($query_vars)
{
$query_vars[] = 'genre';
$query_vars[] = 'type';
$query_vars[] = 'ayear';
return $query_vars;
}
<form role="search" method="get" id="searchform" action="<?php echo bloginfo( 'url' ); ?>">
<?php echo my_dropdown('genre', 'genre'); ?>
<?php echo my_dropdown('type', 'type'); ?>
<?php echo my_dropdown('ayear', 'ayear'); ?>
<input type="submit" id="searchsubmit" value="Submit" />
</form>