0

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", "");
        }

1 Answer 1

1

I'm half asleep here so correct me if I am wrong here but it looks to me like you are saving the publish date of the post each time you save the post,

$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());

This -> $event_end_date->getTimestamp() being the culprit.

So regardless of your variable $wr_event_end_date value being set or not, your meta value is being assigned via getTimestamp() because firstly there is no conditional statement that checks for the existence of your,

$_POST['wr_event_end_date'] 

which if FALSE will instead revert to '' (no value) which is then passed to your

new DateTime($wr_event_end_date);

Secondly, because

new DateTime( //is now empty here );

your

getTimestamp()

returns the current time stamp of the post.

UPDATE

You should still use the text_date custom field, example:

array(
    'name' => 'Test Date Picker',
    'desc' => 'field description (optional)',
    'id'   => $prefix . 'test_textdate',
    'type' => 'text_date',
),

...instead of using a timestamp especially if you have no reason to use a timestamp other than to convert it back to a regular date format because WordPress has inbuilt functions for handling date and time formatting.

Here is an example;

global $post;
$text = get_post_meta( $post->ID, '_cmb_test_textdate', true );
$text = date('d F Y', $text); 
echo $text;
    //prints 25 September 2012 for example

I've made sure to test this library to ensure it is in fact outputting results correctly and it sure is.

12
  • Oh, wow, this must be it. But I'm pretty new to php, how can I make sure it is only save if there is a value in the box? Commented Sep 28, 2012 at 14:17
  • Where are you placing your update_post_meta function and are you trying to save values from a front end form? If you're not, then you really don't need to have an update_post_meta function because the meta box library you are using handles the saving of fields and their values when in the post edit screen. So instead you should only enter a value if you want to, if not, then the field will remain blank until you provide a value.
    – Adam
    Commented Sep 28, 2012 at 14:35
  • Well, I'm kind of confused now. I don't need to save the details of the meat-boxes? I didn't know that the lib handles this as well … see a snippet of my code … cl.ly/code/2F2B2R102B3P … I save all the details for my custom post type with update_post_meta() and retrieve them via a custom get_event_something() function. Can you provide a little update on how I really do that with the library? Thank you in advance! Commented Sep 28, 2012 at 15:57
  • Unless you have the need to do something else with the data prior to saving it WHICH does not consist of simply saving it as you entered it within the post edit screen, then I do not see the need for a custom function hooked onto the save_post action because the library handles the saving of data for you already. Refer to init.php. Your custom function to retrieve the post meta is on the other hand OK because it may serve your need to customize output or simplify a verbose process.
    – Adam
    Commented Sep 28, 2012 at 16:03
  • I suggest temporarily removing (commenting out) your save_details function as shown in your link above and experiment with the meta box in question to see how the library natively handles the the input data for you by default.
    – Adam
    Commented Sep 28, 2012 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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