I have no idea why this would/could happen.
I have a custom metabox for an "event_end_date" set for a custom-post-type. If I leave this field blank it always puts out the publish-date of the post itself?
I use the Custom-Metaboxes-and-Fields-for-WordPress library like this …
$meta_boxes[] = array(
'id' => 'event_info_optional',
'title' => 'Optional',
'pages' => array( 'wr_event'),
'context' => 'normal',
'show_names' => true,
'priority' => 'core',
'fields' => array(
array(
'name' => 'End time of event',
'id' => $prefix . 'event_end_time',
'type' => 'text_time',
)
),
);
And I save the value like this …
$wr_event_end_date = isset($_POST['wr_event_end_date']) ? $_POST['wr_event_end_date'] : '';
$event_end_date = new DateTime($wr_event_end_date);
update_post_meta($post->ID, "event_end_date", $event_end_date->getTimestamp());
And I query the value like this …
function get_event_end_date($post) {
$date = get_post_meta($post->ID, 'event_end_date');
if ( !empty( $date[0] ) ) {
//only if a value is saved I return the value
return $date[0];
}
}
This works for all other custom fields. Just for my event-end-date it doesn't. If I leave the field empty in my backend and save the post, the post itself always shows the publish-date of the post as event-end-date.
Why and how could this get_event_end_date()
function return the publish-date of the post if I leave it empty. If I set a value to the field it works fine, but if I leave it empty I don't want the function to return anything.
UPDATE
// End-event-date
/*$wr_event_end_date = isset($_POST['wr_event_end_date']) ? $_POST['wr_event_end_date'] : '';
$event_end_date = new DateTime($wr_event_end_date);
update_post_meta($post->ID, "event_end_date", $event_end_date->getTimestamp());*/
if ( isset($_POST['wr_event_end_date']) ) {
die('this is getting called even though the field is submitted blank');
$event_end_date = new DateTime($_POST['wr_event_end_date']);
update_post_meta($post->ID, "event_end_date", $event_end_date->getTimestamp());
} else {
update_post_meta($post->ID, "event_end_date", "");
}