wp_insert_post () ou similaire pour le type de message personnalisé
-
-
Avez-vousenregistré un `type depublicationpersonnalisé`nommé` `custom_post` 'avant d'utiliser cet appel?Have you registered a `custom post type` named as `custom_post` before using this call?
- 0
- 2013-07-18
- Rohit Pande
-
oui c'estenregistréyes its registered
- 0
- 2013-07-18
- rashid
-
peuimporte,celafonctionne,il y avait unbugmineur dans lefichier,cetextrait de codeexactest correct.remplacez simplement «custom_post»par letype depublication de votre choix!never mind, its working, there was a minor bug in the file, this exact snippet is correct. just replace 'custom_post' with post type of your choosing!
- 0
- 2013-07-18
- rashid
-
3 réponses
- votes
-
- 2013-07-18
wp_insert_post () remplira une listepar défaut de ceux-cimais le l'utilisateuresttenu defournir letitreet le contenu sinon le L'écriture de labase de données échouera.
$id = wp_insert_post(array( 'post_title'=>'random', 'post_type'=>'custom_post', 'post_content'=>'demo text' ));
wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.
$id = wp_insert_post(array( 'post_title'=>'random', 'post_type'=>'custom_post', 'post_content'=>'demo text' ));
-
- 2014-06-24
Celapeut êtrefaiten utilisant le code suivant: -
Pour saisir unnouveaumessagepour untypepersonnalisé
$post_id = wp_insert_post(array ( 'post_type' => 'your_post_type', 'post_title' => $your_title, 'post_content' => $your_content, 'post_status' => 'publish', 'comment_status' => 'closed', // if you prefer 'ping_status' => 'closed', // if you prefer ));
Après avoirinséré lemessage,unidentifiant depublication sera retournépar lafonction ci-dessus.Désormais,si vous souhaitez saisir desméta-informations relatives à cepost,l'extrait de code suivantpeut être utilisé.
if ($post_id) { // insert post meta add_post_meta($post_id, '_your_custom_1', $custom1); add_post_meta($post_id, '_your_custom_2', $custom2); add_post_meta($post_id, '_your_custom_3', $custom3); }
It can be done using the following code :-
To enter a new post for a custom type
$post_id = wp_insert_post(array ( 'post_type' => 'your_post_type', 'post_title' => $your_title, 'post_content' => $your_content, 'post_status' => 'publish', 'comment_status' => 'closed', // if you prefer 'ping_status' => 'closed', // if you prefer ));
After inserting the post, a post id will be returned by the above function. Now if you want to enter any post meta information w.r.t this post then following code snippet can be used.
if ($post_id) { // insert post meta add_post_meta($post_id, '_your_custom_1', $custom1); add_post_meta($post_id, '_your_custom_2', $custom2); add_post_meta($post_id, '_your_custom_3', $custom3); }
-
Leparamètre «meta_input»peut être utilisé dans letableau «wp_insert_post»,pour ajouter des champsméta,au lieu d'utiliser «add_post_meta»par la suite.The 'meta_input' parameter can be used in the 'wp_insert_post' array, to add meta fields, instead of using 'add_post_meta' afterward.
- 0
- 2019-03-10
- AncientRo
-
Mercipour lepost.@AncientRow,pouvez-vousfournir unexempleincluant `meta_input`?Thanks for the post. @AncientRow, can you provide an example including `meta_input`?
- 0
- 2020-04-22
- Pegues
-
- 2015-03-10
J'aitrouvé que l'utilisation de
isset()
m'apermis d'utiliserwp_insert_post()
sur destypes demessagespersonnalisés:if ( !isset( $id ) ) { $id = wp_insert_post( $new, true ); }
I found using
isset()
allowed me to usewp_insert_post()
on custom post types:if ( !isset( $id ) ) { $id = wp_insert_post( $new, true ); }
-
Pourformater correctement le code dans votre réponse (ou question),mettez-laen surbrillanceet cliquez sur ** {} ** au-dessus de la zone d'édition.To properly format code in your answer (or question), highlight it and click **{}** above the edit box.
- 0
- 2015-03-10
- Gabriel
Besoin d'insérer des objets detype depublicationpersonnalisés àpartir du code.Jen'aipaspu ajouteren utilisant laméthodepar défaut
crée à laplace unmessage régulier.