8

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);
19

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);
  • Thanks Rob! Adding $postdate = date('2010-02-23 18:57:33'); actually makes input boxes stop functioning, perhaps that's just a bug in Chrome though... – torinagrippa Dec 22 '11 at 23:17
  • 1
    I've tried it myself and it works. Maybe your problem is somewhere else in your code. – Rob Vermeer Dec 22 '11 at 23:28
  • I tried using that date formating, and it is returning Notice: A non well formed numeric value encountered in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 4028 – KeepMove Nov 3 '13 at 13:49
  • 2
    should be $postdate = '2010-02-23 18:57:33';, because date() requires literal date format to process, not numbers. Or $postdate = date('Y-m-d H:i:s', strtotime('2010-02-23 18:57:33')); – Alex K Jan 13 '14 at 8:32
3

to convert your date into Wordpress (MySQL DATETIME) format, try this:

$date_string = "Sept 11, 2001"; // or any string like "20110911" or "2011-09-11"
// returns: string(13) "Sept 11, 2001"

$date_stamp = strtotime($date_string);
// returns: int(1000166400)

$postdate = date("Y-m-d H:i:s", $date_stamp);
// returns: string(19) "2001-09-11 00:00:00"

$new_post = array(
    // your other arguments
   'post_date'     =>   $postdate
);

$pid = wp_insert_post($new_post);

or course if you want to really be sexy do this:

'post_date'     => date("Y-m-d H:i:s", strtotime("Sept 11, 2001"))
  • This is very helpful for formatting a Unix timestamp, specifically the date("Y-m-d H:i:s", $date_stamp) code. – David Sep 25 '17 at 21:26
2

You can't format the $_POST['date'] like this... You'll have to run the value from $_POST['date'] through something like $postdate = date( $_POST['date'] )... There's also the possibility to call get_option for the blog settings. See Option Reference in Codex.

  • Using Date actually broke the posting and would return a 404 error. Thanks Kaiser for the direction though! – torinagrippa Dec 23 '11 at 16:17
1

For the community here is my final working code:

header

$year = $_REQUEST['year'];
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$postdate =  $year . "-" . $month . "-" . $day . " 08:00:00";

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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