Formattazione corretta di post_date per wp_insert_post?

23 dic 2011, 00:55:29
Visualizzazioni: 59.5K
Voti: 16

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);
0
Tutte le risposte alla domanda 5
4
30

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);
23 dic 2011 01:08:25
Commenti

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ò...

MTT MTT
23 dic 2011 01:17:56

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

Rob Vermeer Rob Vermeer
23 dic 2011 01:28:50

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

Ari Ari
3 nov 2013 15:49:07

dovrebbe essere $postdate = '2010-02-23 18:57:33';, perché date() richiede un formato di data letterale da elaborare, non numeri. Oppure $postdate = date('Y-m-d H:i:s', strtotime('2010-02-23 18:57:33'));

Alex K Alex K
13 gen 2014 10:32:48
1

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"))
19 ago 2013 17:22:54
Commenti

Questo è molto utile per formattare un timestamp Unix, in particolare il codice date("Y-m-d H:i:s", $date_stamp).

David David
26 set 2017 00:26:18
1

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.

23 dic 2011 13:03:00
Commenti

L'uso di Date ha effettivamente interrotto la pubblicazione e restituiva un errore 404. Grazie comunque a Kaiser per l'indicazione!

MTT MTT
23 dic 2011 18:17:29
0

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
);
23 dic 2011 18:19:27
0

ho trovato questo tramite Google. So che è vecchio ma non c'è una risposta definitiva. Il codice di WordPress utilizza current_time( 'mysql' ) per salvare data/ora nella funzione wp_update_post! Questo genererà il formato data desiderato.

24 feb 2020 11:24:36