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.
$_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.