I found the following function to add a custom taxonomy dropdown to a search form. It works fine for searching, but it does not add the "selected" class to the searched-for term. Here is my code for the taxonomy named grades
:
function get_terms_grades_dropdown($taxonomies, $args)
{
global $search_values;
$myterms = get_terms($taxonomies, $args);
$output = "";
foreach ($myterms as $term) {
$root_url = get_bloginfo('url');
$term_taxonomy = $term->taxonomy;
$term_slug = $term->slug;
$term_name = $term->name;
$value = $term->term_id;
if ($search_values['grades'] == $term->slug) {
$selected = "selected='selected'";
} else {
$selected = '';
}
$output .= "<option value='" . $value . "' " . $selected . ">" . $term_name . "</option>";
}
return $output;
}
Then I use the function like this:
<select name="grade" id="grade_search">
<option value="" selected="selected">Grade Level</option>
<?php
$taxonomies = array('grades');
$args = array('orderby'=>'count','hide_empty'=>true);
echo get_terms_grades_dropdown($taxonomies, $args);
?>
</select>
The issue is with this line:
if ($search_values['grades'] == $term->slug) {
I need to get $search_values['grades']
to equal the searched-for term.
Thank you!