Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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 = date('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);
share|improve this answer
    
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... –  mattrepublic 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 –  Ari Susanto Nov 3 '13 at 13:49
    
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 Jan 13 at 8:32
add comment

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.

share|improve this answer
    
Using Date actually broke the posting and would return a 404 error. Thanks Kaiser for the direction though! –  mattrepublic Dec 23 '11 at 16:17
add comment

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"))
share|improve this answer
add comment

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
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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