Dropdown Menu of All Terms in Custom Taxonomy – WordPress

Built-in to WordPress is a nifty function, wp_dropdown_categories, that is supposed to list all your categories in a drop-down menu and, with the right script, link directly to them. It even has an argument for using it with a custom taxonomy. The only thing is, it doesn’t work. It doesn’t produce the appropriate link. It appears to produce a link like /?cat=# which you would assume would work if you weren’t using pretty permalinks. Only it doesn’t . It never works. I’m not sure why this hasn’t been addressed but it is a serious hole in this function.

Alternately, you can create just a list of parent-level terms with links to their pages and no dropdown menu.

If you’d like to indent the subcategories, click here.

So how do we make this work?  After some digging online, I found this script. It was originally developed here years ago, but it was brought back to life here. And it works brilliantly. Here’s the code:

To make this work for your taxonomy, replace “custom-tax” near the top with the name of your custom taxonomy. Then near the bottom, replace “/category/” with the slug for your custom taxonomy. In my case, I replaced it with “/location/”. Drop this right in, and it should work great!You can adjust the $args by putting them in get_categories(). You can view the WordPress Codex on that function here. Simple stuff!I personally spent many many hours trying to make the regular wp_dropdown_categories working for me, but to no avail. Finding this script made my life so much easier, and hopefully it will do the same for you.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

27 Comments on “Dropdown Menu of All Terms in Custom Taxonomy – WordPress”

  1. This is awesome, was breaking my head whole day how to make this happen, thank you for the solution!
    One question, does anyone know how to make the this list appear in ASC or DESC order?

  2. Do you know how to force the selected taxonomy to stay selected in the dropdown when you end up on the tax page? So if I select ‘circus’, when the tax archive page loads it would show circus in the drop down as being selected.

    1. Yes, you could use this to get the taxonomy ID: get_queried_object()->term_id;

      Set that to a variable, and then add code so that if it’s the same as the term in the dropdown, to add “selected” to the option html. That should make sure it’s pre-selected!

  3. Do you know how to force the selected taxonomy to stay selected in the dropdown when you end up on the tax page? So if I select ‘circus’, when the tax archive page loads it would show circus in the drop down as being selected.

    1. Yes, you could use this to get the taxonomy ID: get_queried_object()->term_id;

      Set that to a variable, and then add code so that if it’s the same as the term in the dropdown, to add “selected” to the option html. That should make sure it’s pre-selected!

  4. for those who ask how to make it work for custom taxonomy terms instead, simply replace get_categories() at the start with get_terms(). You can copy paste the example from the official wordpress documentation:

    $terms = get_terms( array(
    ‘taxonomy’ => ‘post_tag’,
    ‘hide_empty’ => false,
    ) );

    and rename all instances of “$categories with $terms” for clarity.

  5. Hi Brian, Thank you for posting this. I’m a newbie and a bit lost with how to proceed… Could you give me a clue on how to implement this menu? Where should this code reside? A module? A function?

    1. Hi Nick,

      You would place this is a template file in your theme (or child theme if you have one). So maybe you create a custom template file that just displays this dropdown, you would put it in there. You may want to read up on working with custom templates.

  6. Hi there, looks promising. I’m looking for an elegant way to present the tag options on a webpage. Dropdown menu is a possibility, or a nice, clean, customizable box displaying the tags is an option too. I don’t like the standard tag clouds. What are the options? Is there a link were I can actually see what your dropdown menu here looks like? Thanks in advance

  7. Hi Brian,
    Thank’s, this is exactly what i was looking for ! I try to put a second dropdown but it doesn’t work, i have two taxonomies and i really need to have two dropdown. Do you have a solution to this problem ?

  8. Hi Brian,

    This is so close to what I’m looking for! Do you know if there’s a way to modify it for a term of the custom taxonomy instead? For example, if the custom taxonomy used here was “state” and each state had sub-categories or child terms for “cities” in that state, is there a way to have a drop down for all cities of a specific state?

  9. Very nifty and useful! On question though…How can I limit the number of items in the dropdown (only show say, the last 5)?

    1. Try adding something like this:

      [php]
      $i=0;
      foreach($categories as $category){
      if($i==5) break;
      if($category->count > 0){
      $select.= "<option value=’".$category->slug."’>".$category->name."</option>";
      }
      }
      [/php]

      That should limit it to 5. Though I believe in this case, it would display the first 5, and then stop. Let me know if it works!

  10. I think you just need to tweak the default-widgets.php where is located the link pointing to /?cat=xxxx and change to /category/

  11. Thanks Brian !! , you solve my problem with little bit tweaking on those code i made it work !

  12. It is not working for me.

    I think this script work only for categories, not for custom taxonomy.

    Maybe I did some mistake

      1. Can you please help on how to have the category select have the value corresponding to its selected page ? For example if we use the select drop down to navigate to book categories then in the book categories page the select should show “book” instead of the default select. Pls help

      2. Can you please help on how to have the category select have the value corresponding to its selected page ? For example if we use the select drop down to navigate to book categories then in the book categories page the select should show “book” instead of the default “select category”. Pls help

Leave a Reply

Your email address will not be published. Required fields are marked *