Formatage correct de post_date pour wp_insert_post?
5 réponses
- votes
-
- 2011-12-22
Si vousn'ajoutezpas depost_date,WordPress le remplit automatiquement avec la dateet l'heure actuelles.
Pour définir une autre dateet heure
[ Y-m-d H:i:s ]
est labonne structure.Unexemple ci-dessous avec votre 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);
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);
-
Merci Rob!L'ajout de `$postdate=date ('2010-02-23 18:57:33');`faiten fait cesser defonctionner lesboîtes de saisie,peut-être que cen'est qu'unbogue dans Chrome ...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...
- 0
- 2011-12-22
- m-torin
-
Je l'aiessayémoi-mêmeet celafonctionne.Peut-être que votreproblèmeest ailleurs dans votre code.I've tried it myself and it works. Maybe your problem is somewhere else in your code.
- 1
- 2011-12-22
- Rob Vermeer
-
J'aiessayé d'utiliser ceformatage de date,et il renvoie `Remarque: Une valeurnumériquenonbien formée rencontrée dans C: \ xampp \ htdocs \ wordpress \ wp-includes \functions.php sur la ligne 4028`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`
- 0
- 2013-11-03
- Ari
-
devrait être `$postdate='2010-02-23 18: 57: 33';`,car `date ()`nécessite unformat de date littéralpourtraiter,pas desnombres.Ou `$postdate=date ('Y-m-d H:i: s',strtotime ('2010-02-23 18:57:33'));`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'));`
- 2
- 2014-01-13
- Alex K
-
- 2013-08-19
pour convertir votre date auformat Wordpress (MySQL DATETIME),essayez ceci:
$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);
oubien sûr,si vous voulez vraiment être sexy,faites ceci:
'post_date' => date("Y-m-d H:i:s", strtotime("Sept 11, 2001"))
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"))
-
Ceciesttrès utilepourformater un horodatage Unix,en particulier le code `date (" Y-m-d H:i: s ",$ date_stamp)`.This is very helpful for formatting a Unix timestamp, specifically the `date("Y-m-d H:i:s", $date_stamp)` code.
- 0
- 2017-09-25
- David
-
- 2011-12-23
Vousne pouvezpasformater la
$_POST['date']
comme ceci ... Vous devrezexécuter la valeur depuis$_POST['date']
par quelque chose comme$postdate = date( $_POST['date'] )
... Il y a aussi lapossibilité d'appelerget_optionpour lesparamètres dublog.Voir la référence des options dans le Codex.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.-
L'utilisation de Date aen faitinterrompu lapublicationet renvoyait uneerreur 404.Merci Kaiserpour la direction cependant!Using Date actually broke the posting and would return a 404 error. Thanks Kaiser for the direction though!
- 0
- 2011-12-23
- m-torin
-
- 2011-12-23
Pour la communauté,voicimon code detravailfinal:
en-tête
$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 );
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 );
-
- 2020-02-24
esttombé sur Google.Je sais que c'est vieuxmaisiln'y apas de réponse définitive.le code wordpress utilise
current_time( 'mysql' )
pourenregistrer la date/heure dans lafonction wp_update_post!celagénérera leformat de date souhaité.came across thru google. i know its old but there is no definite answer. wordpress code uses
current_time( 'mysql' )
to save date/time in wp_update_post function! this will generate the desired date format.
Quelleest labonnefaçon de définir la date depublication lors de la soumission d'unmessage depuis lefront-enden utilisant wp_insert_post ( Trac )?
Monextrait de codeestmaintenantpublié avec l'heure demysql ...