Custom Post Type – Taxonomy Dropdown Menu?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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.

Method 3

I answered this question on a different post:
Saving Taxonomy Terms


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x