I am using the following code, which allows me to create a dropdown list of terms in my custom taxonomy for use in searching.
This code works just fine for creating the dropdown and using it to search. The problem is, I can’t get it to keep the selected term “selected” in the select box on the results page.
I copied the code from the answer to this question here (https://wordpress.org/support/topic/plugin-events-manager-searching-by-custom-taxonomy#post-3792604), which should add the selected class to the searched term — but it’s not doing anything.
Can you tell by my code what I’ve done wrong? The issue appears to be with the line $search_values[‘grades’] == $term->slug. My custom taxonomy is ‘grades’.
function get_terms_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;
}
Try using just selected which generating the option like below instead of selected = ‘selected’. Should solve it: http://www.w3schools.com/tags/att_option_selected.asp