Tell me more ×
WordPress Answers 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

3 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

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

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

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.