Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a custom post type and added various meta boxes/fields to this custom post type. All is working excellent except for one element...

Instead of utilizing the default interface for selecting a taxonomy I would like to just have a drop down menu for the user to select from.

The idea here is to enable the admins to add taxonomy elements which can be managed centrally however for a specific post to only be associated with one taxonomy.

Further more, I would prefer to just add this drop down into one of my existing meta boxes.

Does anyone happen to have any sample code which would enable me to complete this task?

share|improve this question
    
Is the existing meta box you want to modify one of your own, or one of the core boxes? –  Jan Fabry Sep 3 '10 at 9:28
    
one of my own... I used the WPAlchemy metabox code to create them but I guess that does not matter. –  NetConstructor.com Sep 3 '10 at 22:18
    
does anyone has a response to this by chance? bump? –  NetConstructor.com Sep 6 '10 at 0:03

3 Answers 3

This is how I did this.

<?php $tax = get_object_taxonomies('TAXONOMY_NAME');
    $taxterms = get_terms( $tax, 'orderby=count&offset=1&hide_empty=0&fields=all' );
?>
<select name='tax' id='tax'>
    <option value='' <?php if (!count( $names )) echo "selected";?>>Select Term</option>
    <?php 
    foreach ( $taxterms as $term ) { 
        echo '<option value="' . $term->slug . '" selected>' . $term->name . '</option>',"\n"; 
    } ?>
</select>
share|improve this answer

I don't have code to do this, but it should be simple: create a dropdown named tax_input[your_taxonomy_name], where the values are id's if your taxonomy is hierarchical (like categories), values if not (like tags). If you use this name, I think it is saved automatically, without extra code from you. You can create the dropdown with the wp_dropdown_categories function, pass the selected option with the taxonomy term that should be selected. The callback function that creates the meta box gets the $post parameter, so you can get the current taxonomy term from there.

To disable the meta box that would normally be added, you could set show_ui to false when creating the taxonomy, or remove the meta box before it is drawn (I think the add_meta_boxes hook is a good place). It will have the id tagsdiv-your_taxonomy_name if it is not hierarchical, or your_taxonomy_namediv if it is.

share|improve this answer
    
I would really appreciate if someone might be able to help me out a bit further here as I don't know how I am supposed to save the new taxonomy term when the past is saved and how to show the currently assigned taxonomy term when the post edit page is loaded. –  NetConstructor.com Sep 3 '10 at 6:03
    
Anyone have a solution to this question. The above proposed code is not working as far as I can tell. Essentially, I am confused on how a new dropdown list of existing terms can be created for a specific taxonomy and have that dropdown added to a custom post type. More importantly, how is the data saved when the post is updated and how is the data updated without having the taxonomy add an additional term. –  NetConstructor.com Sep 6 '10 at 15:28
    
The code I posted above has worked perfectly fine for me with either hierarchical or non-hierarchical taxonomies. I use it on New2WP with my 'syntax' taxonomy used on the snippets post type I made. And I use it for the front-end post forms that are used on WPHonors. –  jaredwilli Jan 31 '11 at 16:33

Your Answer

 
discard

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.