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 an interesting problem which I hope someone can quickly answer.

I have created my own metabox which, based on "MY METABOX CODE" (list below) is correctly displaying a dropdown list of all my terms within the "event_types" taxonomy I created.

Where I am running into an issue is being able to SAVE/UPDATE the term associated with a post when a different term is selected from the dropdown and the post is updated.

After tinkering around with various code bits I was able to figure out that by MANUALLY entering the term_ID number(s) [separated by commas] into the array area I am getting the results I am looking for.

For example if on saving the post a function were to call this code

wp_set_post_terms( $post_id, array(5,7), 'event_types', FALSE);

then my post WILL UPDATE and associate the term_ID 5 & 7 to it which as you can see I am passing in the array. The problem is that this is hard coded into my functions.php file and not based on a user selected dropdown value (note: I am actually only trying to pass one ID but I did two for testing as explained below).

I have also been able to figure out that if I add the following code to my metabox file then I am able to echo a list of assigned term IDs but the last item has a comma.

<?php $event_types = wp_get_object_terms($post->ID, 'event_types'); 
foreach ($event_types as $event_type) { echo $event_type->term_id . ','; } ?>

So... it seems I have 85% of my problem solved. The Remaining 15% of my problem remains as follows:

  1. What do I need to add to my functions.php file code (listed below) so that when I create/update a post the NEW VALUE selected from my taxonomy dropdown list is passed into the array?

  2. Although in this example I am looking to ensure that only a SINGLE taxonomy can be associated with a post, there are other situations where I would like to create a checkbox list which would require me to pass more than one value into the array. As such, what would I need to change so that a comma separated list of term IDs is passed into the array? If your answer involves using some or part of the example code I listed above where I echo the IDs then how do I ensure the last ID which is printed does not have a comma at the end? (Is there a different/better way to do it? I saw hints which might relate to adding a filter but I am not sure how this is done...)

Thank you guys very much in advance - below is the code I am currently using in each file.

CODE IN MY FUNCTIONS.PHP FILE

function save_event_taxonomy_terms($meta, $post_id) {
        $event_types = wp_get_object_terms($post->ID, 'event_types'); 
        wp_set_post_terms( $post_id, array($names), 'event_types', FALSE);
    }

MY METABOX CODE

<select name='post_event_types' id='post_event-types'>
// DISPLAY TERMS AS DROP DOWN OPTIONS
    <?php 
    $names = wp_get_object_terms($post->ID, 'event_types'); 
    $event_types = get_terms('event_types', 'hide_empty=0'); 
    ?>
    <option class='event_type-option' value='' <?php if (!count($names)) echo "selected";?>>Not Assigned</option>
    <?php foreach ($event_types as $event_type) {
        if (!is_wp_error($names) && !empty($names) && !strcmp($event_type->slug, $names[0]->slug)) 
        echo "<option class='event_type-option' value='" . $event_type->slug . "' selected>" . $event_type->name . "</option>\n"; 
        else
        echo "<option class='event_type-option' value='" . $event_type->slug . "'>" . $event_type->name . "</option>\n"; 
    }
    ?>
</select>
share|improve this question

1 Answer 1

up vote 6 down vote accepted

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>
share|improve this answer

protected by kaiser Apr 3 at 13:21

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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