I've been searching for documentation or examples on how to modify the output of wp_dropdown_categories so I can add the term ID to each option. Is this possible? If not, my only thought would be to try to do so with jQuery which doesn't seem as reliable or as fast.

Here's my current code:

<div class="location-cats-dropdown">
    <form id="location-category-select" class="location-category-select" method="get">
        <?php wp_dropdown_categories(
            'orderby'         => 'menu_order',
            'taxonomy'        => 'location_category', // Taxonomy slug
            'name'            => 'location_category', // Taxonomy slug
            'value_field'     => 'slug',
            'show_option_all' => 'Make a selection',
            'selected'        => km_get_selected_taxonomy_dropdown_term(), // Set which option in the dropdown menu is the currently selected one
        ); ?>
        <input type="submit" name="submit" value="Filter" />

And here's the current output:

<select name="location_category" id="location_category" class="postform">
    <option value="0">Byway Categories</option>
    <option class="level-0" value="accommodations">Accommodations</option>

I'm looking to add the term ID to each option like this example output:

<option class="level-0" id="term-id-48" value="accommodations">Accommodations</option>

Thanks in advance for any info!

  • That function is just a helper function, have you considered cutting out the middle man and just generating the markup yourself? – Tom J Nowell Feb 25 '20 at 16:34
  • Would you suggest using a get_terms query? – Greg Feb 25 '20 at 18:21
  • get_terms ended up a much better and more customizable solution. – Greg Feb 25 '20 at 18:47

get_terms ended up bring a more customizable solution which is working for my needs. I was able to easily add the term IDs to each option as needed. Hope this helps someone else.

<div class="location-cats-dropdown">
    <form id="location-category-select" class="location-category-select" method="get">
        <?php $loc_cats = get_terms( array(
            'taxonomy' => 'location_category',
            'hide_empty' => true,
            'orderby' => 'menu_order',
            'order' => 'ASC',
        ) );
        if ( $loc_cats ) : ?>
            <select name="location_category" id="location_category" class="postform">
                <option value="0">Byway Categories</option>
                <?php foreach( $loc_cats as $loc_cat ) : ?>
                    <option value="<?php echo $loc_cat->slug; ?>" id="term-id-<?php echo $loc_cat->term_id; ?>"><?php echo $loc_cat->name; ?></option>
                <?php endforeach; ?>
        <?php endif;
        wp_reset_postdata(); ?>
        <input type="submit" name="submit" value="Filter" />
  • upvoted for self-answering! Glad you found a solution :) – Tom J Nowell Feb 25 '20 at 21:00
  • Thank you! I'll accept my own answer when that option becomes available in a day. And thank you for providing the idea to move on from wp_dropdown_categories. I really didn't know what limitations there were. Cheers! – Greg Feb 26 '20 at 13:47

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.