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>
    $taxonomies = array('grades');
    $args = array('orderby'=>'count','hide_empty'=>true);
    echo get_terms_grades_dropdown($taxonomies, $args);

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!


1 Answer 1


$seach_values can be changed whenever the site redirect, be sure that $search_values still keeps the input values from your search form.

To be sure I suggest use GET or POST to pass the values from form. So, your comparing statement should be

$_GET['grades'] == $term->slug

Here I suppose your search form use method="GET"

  • Thank you. I replaced with your code above, but unfortunately did not solve the issue. I'm using this to add a taxonomy search to the "Events Manager" plugin. The code I found comes from "pharmadave" here: wordpress.org/support/topic/…
    – LBF
    Commented Apr 20, 2015 at 18:05
  • So, did you remember to Set '$search_values['cat'] = 'your chosen items slug' before call that function?
    – Trang
    Commented Apr 21, 2015 at 1:28
  • I tried both $search_values['cat'] = 'grades' before the function AND directly as shown in my code above $search_values['grades'] -- neither is working. I don't what's going wrong because the rest of the search works.
    – LBF
    Commented Apr 21, 2015 at 2:31
  • I'm not even sure what $search_values[] is... it's referenced as a global variable, but I can't find a reference to it anywhere in WordPress or the core Events Manager plugin...?
    – LBF
    Commented Apr 21, 2015 at 3:01
  • Could you show me the result if you add vardump($search_values); vardump($term->slug); right after foreach starts (After line: foreach ($myterms as $term) {)
    – Trang
    Commented Apr 21, 2015 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.