Proper formatting of post_date for wp_insert_post?

What is the proper way to define the post date when submitting a post from the front end using wp_insert_post (Trac)?

My snippet now is publishing with the mysql time…

if (isset ($_POST['date'])) {
    $postdate = $_POST['Y-m-d'];
}
else {
    $postdate = $_POST['2011-12-21'];
}

// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title'    =>   $title,
'post_content'  =>   $description,
'post_date'     =>   $postdate,
'post_status'   =>   'publish',
'post_parent' => $parent_id,
'post_author' => get_current_user_id(),
);

//SAVE THE POST
$pid = wp_insert_post($new_post);

5

If you don’t add a post_date then WordPress fills it automatically with the current date and time.

To set another date and time [ Y-m-d H:i:s ] is the right structure. An example below with your code.

$postdate="2010-02-23 18:57:33";

$new_post = array(
   'post_title'    =>   $title,
   'post_content'  =>   $description,
   'post_date'     =>   $postdate,
   'post_status'   =>   'publish',
   'post_parent'   =>   $parent_id,
   'post_author'   =>   get_current_user_id(),
);

//SAVE THE POST
$pid = wp_insert_post($new_post);

Leave a Comment