I'm trying to save a post meta data on the database, using custom post type. The problem is I can't store the meta_key, the only one storing is the meta-value. I have two textbox there, one for event date and one for event name.. I'm trying to store the date on the meta key and the event on the value. the problem is that it only store on the meta value, and i'm not sure on the meta-key. Also how do you store multiple meta-keys and meta-values on the same post? Any idea which part of this syntax is wrong? thanks :)
function add_calendar_metaboxes() {
add_meta_box('wpt_calendar_location', 'Event Date', 'wpt_calendar_location', 'calendar_holiday', 'normal', 'default');
}
// The Event Location Metabox
function wpt_calendar_location() {
global $post;
echo "<form method=\"POST\">";
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the location data if its already been entered
$event_name = get_post_meta($post->ID, '_event_name', true);
$event_date = get_post_meta($post->ID, '_event_date', true);
echo '<label>Event Date</label><input type="text" name="_event_date" value="' . $event_date . '" />';
echo '<label>Event Name</label><input type="text" name="_event_name" value="' . $event_name . '" />';
echo '<input type="submit" name="Submit">';
echo '</form>';
}
// Save the Metabox Data
function wpt_save_events_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
$events_meta[] = array($_POST['_event_date'] => $_POST['_event_name']);
foreach ($events_meta as $key => $value)
{
if( $post->post_type == 'revision' ) return;
$value = implode(',', (array)$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);
}
}
add_action('save_post', 'wpt_save_events_meta', 1, 2);