Joindre des données de taxonomie à publier avec wp_insert_post
3 réponses
- votes
-
- 2011-05-25
Utilisez
wp_set_object_terms
après avoir obtenu l'identifiant depublicationpour chaquetaxonomie:... $post_id = wp_insert_post( $my_post ); wp_set_object_terms( $post_id, $location, 'location' ); wp_set_object_terms( $post_id, $sale_rental, 'sale_rental' ); wp_set_object_terms( $post_id, $price, 'price' );
Use
wp_set_object_terms
after you have the post id for each taxonomy:... $post_id = wp_insert_post( $my_post ); wp_set_object_terms( $post_id, $location, 'location' ); wp_set_object_terms( $post_id, $sale_rental, 'sale_rental' ); wp_set_object_terms( $post_id, $price, 'price' );
-
Jene saistout simplementpas oùje serais sans votre aide,j'aitravaillé lapremièrefois ...mercibeaucoup Bainternet.Marqué comme réponduI just do not know where i would be without your help, worked first time... many thanks Bainternet. Marked as answered
- 0
- 2011-05-25
- MartinJJ
-
Heureux d'avoirpu aider :)Glad i could help :)
- 0
- 2011-05-25
- Bainternet
-
Lestableaux $ locationet $ sale_rental sont-ils desentiers ou des chaînes?Are $location and $sale_rental arrays of integers or of strings?
- 0
- 2014-10-23
- Doug
-
- 2012-09-17
Vouspouvez lefaireen utilisant wp_insert_post ,mais vous devez également spécifier lataxonomie dans
tax_input
,il devrait donc ressembler à ceci:$item['tax_input'] = array ( 'location' => implode( ',', $location ), 'sale_rental' => implode( ',', $sale_rental ), 'price' => implode( ',', $price ), )
J'utilise
implode()
pour que$location
peut être untableau avecplusieurstermes.Notez également que celane fonctionne quepour lestaxonomiesnon hiérarchiques.Pour lestaxonomies hiérarchiques,vous devezfournir untableau au lieu d'astring.
You can do it using wp_insert_post, but you must specify taxonomy as well in
tax_input
, so it should look like this:$item['tax_input'] = array ( 'location' => implode( ',', $location ), 'sale_rental' => implode( ',', $sale_rental ), 'price' => implode( ',', $price ), )
I use
implode()
so that$location
could be an array with multiple terms.Also, notice that this works only for non-hierarchical taxonomies. For hierarchical taxonomies you must supply an array instead of astring.
-
- 2013-01-25
Source duproblème
Après quelques recherches sur ce sujet,onm'a dit de vérifier les élémentsinternes (ce quej'aifait).Commej'importais des articles àpartir d'unfluxexterneen tant quetype depublicationpersonnalisé,j'ai simplement défini l'utilisateur sur
-1
(au lieu d'ajouter un utilisateur debot).Leproblème quej'ai rencontré était quewp_insert_post()
avec unensembletax_input
,vérifieen interne une capacité utilisateur,ce qu'un utilisateurnonexistantn'a évidemmentpas.SysBotpour le sauvetage
La solution a alors été quej'ai écrit le plugin SysBot .De cettefaçon,je pourrais simplement attacher l'utilisateur SysBot (qui a le rôle d ' éditeur ) à cemessagenouvellement crééet toutfonctionnait commeprévu.
Source of the problem
After some research to this topic, I was told to check the internals (which I did). As I were importing posts from an external feed as custom post type, I simply set the user to
-1
(instead of adding a bot user). The problem I ran into was thatwp_insert_post()
with atax_input
set, internally checks for a user capability, which a non existing user obviously doesn't have.SysBot for the rescue
The solution then was that I wrote the SysBot plugin. This way I could simply attach the SysBot user (which has the role of editor) to that newly created post and everything worked the way it was expected.
J'essaie demettreen œuvre un système depublicationfrontal qui affiche les données detaxonomie dansplusieurs champs de sélection déroulants. Chacun desmenus déroulantsestnomméen utilisant le "nom"
$arg
danswp_dropdown_categories()
.Comme vouspouvez le voir,lataxonomieest "location"et lenom sélectionnéest également "location".
J'ajouteensuite les variablespour chacune des listes déroulantes de sélection detaxonomie ainsi quepost_title,post_contentetc:
Enfin,j'ajoute lesinformations supplémentaires dans untableauprêt à êtreenvoyépar
wp_insert_post()
. Jene saispas sije fais labonne choseen ajoutanttax_input
dans letableau comme ci-dessous car c'est ce queje comprends du codex queje doisfaire.Pour quetout ressemble à ceci:
Cependant,lorsquej'ai soumis lenouveaumessage,toutes les données depublication standard (ainsi quemontype depublicationpersonnalisé) sont correctes,mais lestaxonomiesne lefontpas. Jefais évidemment quelque chose demalmais quoi?