Custom taxonomy drop down

Here is a quick example that will help you create a drop down menu, populated with all of the terms from a custom taxonomy.

This code is mostly from this thread on WordPress.com forums, I’ve just changed a few things for my situation.

Here’s the function that does most of the hard work.

function get_terms_dropdown($taxonomies, $args){
	$myterms = get_terms($taxonomies, $args);
	$output ="<select name='buscategory'>";
	$output .="<option value='#'>Please select a business category</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;
}

Then place this code where you would like to call the drop down menu, this could be using a hook or in your template files.

<form action="<?php bloginfo('url'); ?>" method="get">
<p>Or search for a service from the drop down menu:</p>
<?php
$taxonomies = array('buscategory');
$args = array('orderby'=>'name','hide_empty'=>true);
$select = get_terms_dropdown($taxonomies, $args);

$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
	<noscript><div><input type="submit" value="Go" /></div></noscript>
	</form>
	</div>

You’ll need to update the name of the taxonomy for this to work, you’ll see in the above example I’ve used ‘buscategory’ it needs to be update in both code blocks.

Say Hello!

Got a WordPress or Digital Marketing Project that you'd like to discuss? I'm interested in hearing about new projects and where I might be able to assist. Please visit contact page here.
  • Petra

    I tried your code, but I don’t know why doesn’t work for me. I changed the name of taxonomy to my taxonomy, which is ‘subjects’, but I just get a dropdown with nothing in it. Any ideas?

    • Peter

      Hi Petra, did you put the function “get_terms_dropdown($taxonomies, $args)” into your site as well? Probably in the functions.php file ??

      Also ensure that you have PHP errors turned on so that you see any problem when you execute the form.

      Let me know how you go

      Peter

  • Petra

    Thanks for your quick reply Peter!
    In my functions.php I have this:

    function get_terms_dropdown($taxonomies, $args){
    $myterms = get_terms($taxonomies, $args);
    $output =””;
    $output .=”Please select a subject”;
    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 .=””.$term_name.””;
    }
    $output .=””;
    return $output;
    }

    And in my archives-video.php I have this:
    <form action="” method=”get”>

    ‘name’,’hide_empty’=>true);
    $select = get_terms_dropdown($taxonomies, $args);

    $select = preg_replace(“#]*)>#”, “”, $select);
    echo $select;
    ?>

    I put the name of my taxonomy here -> $taxonomies = array(‘subjects’);

    Where I am registering my taxonomy I have this code:

    //subject
    $subjectlabels = array(
    ‘name’ => _x( ‘Subjects’, ‘taxonomy general name’ ),
    ‘singular_name’ => _x( ‘Subject’, ‘taxonomy singular name’ ),
    ‘search_items’ => __( ‘Search Subjects’ ),
    ‘all_items’ => __( ‘All Subjects’ ),
    ‘parent_item’ => __( ‘Parent Subject’ ),
    ‘parent_item_colon’ => __( ‘Parent Subject:’ ),
    ‘edit_item’ => __( ‘Edit Subject’ ),
    ‘update_item’ => __( ‘Update Subject’ ),
    ‘add_new_item’ => __( ‘Add New Subject’ ),
    ‘new_item_name’ => __( ‘New Subject Name’ ),
    ‘menu_name’ => __( ‘Subjects’ ),
    );
    register_taxonomy(‘subjects’, ‘videos’, array (
    ‘hierarchical’ => true,
    ‘labels’ => $subjectlabels,
    ‘show-ui’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => array (‘slug’ => ‘subject’)
    ));

    I wonder what I am missing?

  • Petra

    Sorry the code for the dropdown is this (didn’t paste it correctly):
    <form action="” method=”get”>
    ‘name’,’hide_empty’=>true);
    $select = get_terms_dropdown($taxonomies, $args);
    $select = preg_replace(“#]*)>#”, “”, $select);
    echo $select;
    ?>

  • Petra

    For some reason it strips out part of my code. Here it is on another website:

    http://codepad.org/A5saoAgb

  • Petra

    Good news! i managed to solve the problem. taxonomy name wasn’t really correct and the way it was registered.
    http://codex.wordpress.org/Function_Reference/register_taxonomy

    • Peter

      Hi Petra,

      Just saw your comments, and happily the last one says you got it working – well done!

      Peter

  • Johanna

    Hi Peter,
    great example!

    Is it possible, to show only the first level of the taxonomy?
    And after selection of one of those parents, another drop down with the depending childs is filled?
    And like now, the pages/posts with the selected taxonomy is shown.

    Thanks for your help and answer!
    Johanna

    • Peter

      Johanna, yes that should be possible, you’d have to start tinkering with ajax though…

  • http://www.cssreflex.com Naeem

    I’m trying to add ‘show_count’=> 1 in args, but it doesnt seems to work. what I’m doing wrong?

  • Ross

    In case it helps anyone – I went one step further and created a plugin with drop downs that filter your pages according to your taxonomy selections :) http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/

    • http://thedma.com.au/ Peter Shilling

      Thanks Ross, I’ll check out your plugin!

  • http://www.troytempleman.com Troy Templeman

    Hi Peter, 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?

    • http://thedma.com.au/ Peter Shilling

      Troy,

      Yes you should be able to do this…

      A rough idea, add a new argument to the second code block

      $args = array(‘orderby’=>’name’,’hide_empty’=>true);

      perhaps a new one ‘term’ where you set the term

      Then in the first block of code look for that argument and then only get items that have this term as the parent.

      Hope that helps?