Saving Taxonomy Terms

I figured I would post the answer to this after Dimas was able to assist me.

Utilizing his WPAlchemey Class I added a save_action var which looked like this (note that I am using the taxonomy for “category” which of course you can change to whatever your custom taxonomy might be):

'save_action'   => 'save_taxonomy_terms',

I then add the following function for this as follows:

function save_taxonomy_terms($meta, $post_id) {
wp_set_post_terms($post_id, array($meta['my_terms']), 'category', FALSE);
}

An my metabox code which displays the dropdown list of taxonomies looks like this:

<label>Event Category:</label>
    <?php $terms = get_terms('category', 'hide_empty=0'); ?>
    <?php $mb->the_field('my_terms'); ?>
    <select name="<?php $mb->the_name(); ?>">
    <option value="" <?php if (!count($terms)) echo "selected";?>>Not Assigned</option>
    <?php foreach ($terms as $term): ?>
    <option value="<?php echo $term->term_id; ?>"<?php $mb->the_select_state($term->term_id); ?><?php echo '>' . $term->name; ?></option>
    <?php endforeach; ?>
    </select>

Leave a Comment