Formattazione corretta di post_date per wp_insert_post?
Qual è il modo corretto per definire la data del post quando si invia un post dal frontend utilizzando wp_insert_post (Trac)?
Il mio snippet attualmente sta pubblicando con l'orario mysql...
// Se la data è impostata nel POST
if (isset ($_POST['date'])) {
$postdate = $_POST['Y-m-d'];
}
else {
$postdate = $_POST['2011-12-21'];
}
// AGGIUNGE L'INPUT DEL FORM ALL'ARRAY $new_post
$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(),
);
//SALVA IL POST
$pid = wp_insert_post($new_post);

Se non aggiungi un post_date, WordPress lo completerà automaticamente con la data e l'ora correnti.
Per impostare una data e ora diversa [ Y-m-d H:i:s ]
è la struttura corretta. Di seguito un esempio con il tuo codice.
$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(),
);
//SALVA IL POST
$pid = wp_insert_post($new_post);

Grazie Rob! Aggiungere $postdate = date('2010-02-23 18:57:33');
in realtà fa smettere di funzionare le caselle di input, forse è solo un bug di Chrome però...

L'ho provato io stesso e funziona. Forse il tuo problema è da qualche altra parte nel tuo codice.

Ho provato a usare quel formato di data, e restituisce Notice: A non well formed numeric value encountered in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 4028

per convertire la tua data nel formato Wordpress (MySQL DATETIME), prova questo:
$date_string = "Sept 11, 2001"; // o qualsiasi stringa come "20110911" o "2011-09-11"
// restituisce: string(13) "Sept 11, 2001"
$date_stamp = strtotime($date_string);
// restituisce: int(1000166400)
$postdate = date("Y-m-d H:i:s", $date_stamp);
// restituisce: string(19) "2001-09-11 00:00:00"
$new_post = array(
// altri tuoi argomenti
'post_date' => $postdate
);
$pid = wp_insert_post($new_post);
ovviamente se vuoi essere davvero elegante fai così:
'post_date' => date("Y-m-d H:i:s", strtotime("Sept 11, 2001"))

Non puoi formattare $_POST['date']
in questo modo... Dovrai elaborare il valore da $_POST['date']
attraverso qualcosa come $postdate = date( $_POST['date'] )
... C'è anche la possibilità di chiamare get_option per le impostazioni del blog. Vedi Option Reference nel Codex.

Per la comunità ecco il mio codice funzionante finale:
intestazione
$year = $_REQUEST['year'];
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$postdate = $year . "-" . $month . "-" . $day . " 08:00:00";
$new_post = array(
'post_title' => $title, // Titolo del post
'post_content' => $description, // Contenuto del post
'post_status' => 'publish', // Stato del post (pubblicato)
'post_author' => get_current_user_id(), // ID dell'autore corrente
'post_date' => $postdate // Data del post
);
