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 am working on a site at the moment where I need to save several different types of meta data for a custom post type.

At present I have a text input that is saving with no problem but am struggling to get a select field to do the same.

This is the function that outputs the select field:

function kwi_department_input() {
    // Define function to create meta box for team member department.

    global $post;
    echo '<input type="hidden" name="teammeta_noncename" id="teammeta_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
    $department = get_post_meta($post->ID, '_department', true); ?>

    <select name="_department" id="_department">
        <option value="Directors" <?php selected($department, 'Directors'); ?>>Directors</option>
        <option value="Finance / Admin" <?php selected($department, 'Finance / Admin'); ?>>Finance / Admin</option>
        <option value="Customer Service Team" <?php selected($department, 'Customer Service Team'); ?>>Customer Service Team</option>
        <option value="Commercial Team" <?php selected($department, 'Commercial Team'); ?>>Commercial Team</option>
    </select>
<?php }

And this is the function that saves all of the data:

function kwi_save_team_meta($post_id) {
    if( !wp_verify_nonce( $_POST['teammeta_noncename'], plugin_basename(__FILE__) )) {
        return $post_id;
    }

    if( !current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }

    $team_meta = array(
        '_department' => $_POST['_department'], 
        '_languages' => $_POST['_languages'] // Languages is the text input that is saving with no issue.
    );

    foreach($team_meta as $key => $value) {
        if(get_post_meta($post_id, $key, FALSE)) {
            update_post_meta($post_id, $key, $value);
        } else {
            add_post_meta($post_id, $key, $value);
        }
        if(!$value) {
            delete_post_meta($post_id, $key);
        }
    }
}

Any pointers as to what I'm doing wrong would be much appreciated.

share|improve this question
    
I've investigated further and it seems that the issue is with with Wordpress sending the value of the selected option. $_POST['_department'] is definitely set but just seems to be an empty string. So I think the issue must be with the markup of the select box not capturing which option is selected. –  James O'Neill Apr 25 '13 at 16:21
add comment

1 Answer 1

up vote 1 down vote accepted

Ok, I've solved the issue myself.

Turns out I had the name="_department" attribute duplicated in another field that was conflicting with my select field. My select saves just fine now I've edited that.

share|improve this answer
add comment

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.