Ok, so the answer here is both annoying and fairly difficult to find/fix.
The wp_dropdown_categories()
function outputs the dropdown for each custom taxonomy filter using the term_id
as the value for each option. However, when constructing the query the WP_Query::parse_tax_query()
method, by default, sets the field
element to slug
.
So, I had to make a custom walker and use that with the wp_dropdown_categories()
function, as below –
/**
* Custom extended FGW_Cat_Slug_Walker Class
* Outputs the term 'slug' as the value of each option, not 'term_id' (as is default)
*/
class FGW_Cat_Slug_Walker extends Walker_CategoryDropdown{
/**
* Start the element output
*
* @param required string $output Passed by reference. Used to append additional content
* @param required object $category Category data object
* @param required int $depth Depth of category in reference to parents. Default 0
* @param required array $args An array of arguments
* @param required int $id ID of the current category
*/
public function start_el(&$output, $category, $depth = 0, $args = array(), $id = 0){
$pad = str_repeat(' ', $depth * 3); // Create the padding (before nested terms)
/** Generate the HTML for this option */
$output.= sprintf("\t".
'<option class="%1$s" value="%2$s" %3$s>%4$s%5$s</option>',
/** %1$s - 'class' attribute */ 'level-' . $depth,
/** %2$s - 'value' attribute */ $category->slug,
/** %3$s - 'selected' attribute */ ($category->slug == $args['selected']) ? ' selected="selected"' : '',
/** %4$s - option text */ $category->name,
/** %5$s - The term count */ ($args['show_count']) ? ' (' . $category->count . ')' : ''
);
}
}