WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using this code to get the term_id of a custom taxonomy. The drop down does list the terms of the taxonomy Type. When I print the $terms variable I see the term ID of the post i'm editing on the front end. Why isn't the term ID being set as the selected option?

$terms = get_the_terms( $current_post, 'Type' );
wp_dropdown_categories( 'taxonomy=Type&hierarchical=1&hide_empty=0&id=cat&show_count=0&selected=' . $terms[0]->term_id );

This is the output of the array

Array ( [25] => stdClass Object ( [term_id] => 25 [name] => Sativa [slug] => sativa [term_group] => 0 [term_icon] => http://pothwy.com/wp-content/themes/GeoPlaces/images/default.png [term_price] => 0 [term_taxonomy_id] => 53 [taxonomy] => Type [description] => [parent] => 0 [count] => 4 [object_id] => 563 ) )

Output of HTML

<select name="cat" id="cat" class="postform">
    <option class="level-0" value="25">Sativa</option>
    <option class="level-0" value="52">Indica</option>
share|improve this question
Could you post the dump of the $terms array please? Also, could you dump the HTML of the wp_dropdown_categories thanks :) – Cristian Feb 12 '13 at 8:59
@Cristian updated my answer to include the array and HTML – Anagio Feb 12 '13 at 9:25

That's because you don't have the $terms[0] element. Use following code to get the first key of $terms array:

// Set the pointer to the first element
// you don't need this if there is the only array element
// get the key of the current position
$selected_id = key($terms);

You MUST enable WP_DEBUG when on develop stage to see important information you don't see now:

Notice: Undefined offset: 0 in /home/...
Notice: Trying to get property of non-object in /home/...
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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