0

I want to display one dropdown menu with a list of values for a custom taxonomy. My taxonomy is "issue-year" and the dropdown would look like this:

<select name="issue-year">
    <option value="#">Please select a year</option>
    <option value="2016">2016</option>
    <option value="2015">2015</option>
    <option value="2014">2014</option>
</select>

Once a year is selected, a second dropdown would appear below the first to display posts in a custom post type that match the selected taxonomy value. My custom post type is "newsletter."

<select name="newsletter">
    <option value="#">Please select an issue</option>
    <option value="postname-1">Post Name 1</option>
    <option value="postname-2">Post Name 2</option>
    <option value="postname-3">Post Name 3</option>
</select>

I'm currently using "get_terms_dropdown" to generate the first field, but I'm not sure how to get from step one to step two. Also, if there's a better way than what I did in the code below I'd love to hear it.

function get_terms_dropdown($taxonomies, $args){
    $myterms = get_terms($taxonomies, $args);
    $output ="<select name='issue-year'>";
    $output .="<option value='#'>Please select a Year</option>";
    foreach($myterms as $term){
        $root_url = get_bloginfo('url');
        $term_taxonomy=$term->taxonomy;
        $term_slug=$term->slug;
        $term_name =$term->name;
        $link = $term_slug;
        $output .="<option value='".$link."'>".$term_name."</option>";
    }
    $output .="</select>";
return $output;
}
0

With a combination of jQuery / JavaScript and WP AJAX; you could look for an onchange event on the select box, send an ajax request to your admin-ajax.php with the year and then do a custom WP Query to return the html for the secondary drop down and append it to your dom.

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29

http://api.jquery.com/jquery.ajax

Once it is appending you can target your new select box with an onchange event as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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