Comment enregistrer un tableau avec un metakey dans postmeta?
1 réponses
- votes
-
- 2011-08-06
Vousn'avezpasbesoin deparcourir les valeurs.Utilisez simplement
update_post_meta($post_ID, {key}, {array of vals})
,ça devraitfaire!<?php $poddata = Array( 'pod_id' => $this->pod_id, 'url' => $this->url, 'name' => $this->name, 'description' => $this->description, 'service' => $this->service, 'status' =>$this->status, 'price' => $this->price ); //Update inserts a new entry if it doesn't exist, updates otherwise update_post_meta($post_ID, 'poddata', $poddata); ?>
C'esttout!Lorsque vous le récupérezpour utilisation,procédez comme suit:
$poddata = get_post_meta($post_ID, 'poddata');
$poddataest letableau de valeurs.
You don't need to loop through the values. Just use
update_post_meta($post_ID, {key}, {array of vals})
, it should do!<?php $poddata = Array( 'pod_id' => $this->pod_id, 'url' => $this->url, 'name' => $this->name, 'description' => $this->description, 'service' => $this->service, 'status' =>$this->status, 'price' => $this->price ); //Update inserts a new entry if it doesn't exist, updates otherwise update_post_meta($post_ID, 'poddata', $poddata); ?>
Thats it! When you fetch it for usage, do the following:
$poddata = get_post_meta($post_ID, 'poddata');
$poddata is the array of values.
-
J'aiessayé update_post_meta ($post_ID,'poddata',$postdata),après avoirenregistré lemessage,je vois que lamétan'estpasenregistrée.I tried update_post_meta($post_ID, 'poddata', $postdata), after save the post, i see the meta is not saved.
- 0
- 2011-08-06
- Jenny
-
oh,désolé,il a étéenregistré,je ne l'aipas vu sur letableau des champspersonnalisés WP.Je viens de letrouver dansphpAdmin.Merci!oh, sorry, It saved, I didn't see it on WP Custom Fields Table. I just found it in phpAdmin. Thanks!
- 0
- 2011-08-06
- Jenny
-
Lors de la récupération des données,parget_post_meta ($post ID,'posdata'); J'obtiens array (0) de var_dump.Commentpuis-je obtenir letableau complet?When retrive the data, by get_post_meta($post_ID, 'poddata'); I get array(0) from var_dump. How can I get the whole array?
- 0
- 2011-08-06
- Jenny
-
De rien! Essayez d'utiliserprint_r () ... echo "
"; print_r ($poddata); echo "
";You're welcome! Try using print_r()... echo ""; print_r($poddata); echo "
";- 0
- 2011-08-06
- Rutwick Gangurde
-
print_r ($poddata) affiche Array ()print_r($poddata) shows Array()
- 0
- 2011-08-06
- Jenny
-
Ohh ... alors les valeursne sontpas stockées.Vous obtenez untableau vide.J'aiessayé l'exemple localementet cela abien fonctionné.Essayez unprint_r dutableau de valeurs avant demettre àjour_post_meta.Iln'obtientprobablement aucune valeur.Ohh... then the values aren't stored. You're getting an empty array. I tried the example locally, and it worked fine. Try a print_r of the array of values before you update_post_meta. It's probably not getting any values.
- 0
- 2011-08-06
- Rutwick Gangurde
-
Ilest stocké,je peux le voir dansphpmyAdmin,lesnouveauxmessages ont unmétakey de "pod"et une valeur dutableau.Jene peuxpas le récupérer.It's stored, I can see it in phpAdmin, new posts has metakey of "pod" and and value of the array. Just can't fetch it.
- 0
- 2011-08-06
- Jenny
-
Essayez `$poddata=get_post_meta ($post_ID,'poddata',true);`Try `$poddata = get_post_meta($post_ID, 'poddata',true);`
- 0
- 2011-08-06
- Bainternet
-
Jenny,essayez aussi la solution ci-dessus. @Bainternet J'ai délibérémentignoré letroisièmeparamètre,quiest le drapeau «unique»,car la valeurmétaest untableau dansget_post_meta.Est-cenécessairepourtraiter destableaux au lieu de chaînes commeméta-valeurs?Jene l'utilise que lorsqueje stocke des chaînesen tant queméta-valeurs.Jenny, Try the above solution too. @Bainternet I deliberately skipped the third parameter, which is the 'single' flag, as the meta value is an array in get_post_meta. Is it required when dealing with arrays instead of strings as meta values? I use it only when I store strings as meta values.
- 0
- 2011-08-06
- Rutwick Gangurde
-
Génial!!!le "vrai"fonctionne!Merci!!!Great!!! the "true" works! Thanks!!!
- 0
- 2011-08-06
- Jenny
-
Le vrai 3èmeparamètre doit êtreignoré si vous avezplus d'un champs avec lemêmenom,sa valeurpeut être une chaîne,unbooléen,unentier ou untableaumais cen'estpas de quoiil s'agit.The true 3rd parameter should be skipped if you have more then one fields with the same name, it value can be a string,boolean,integer or array but that is not what this parameter is about.
- 0
- 2011-08-06
- Bainternet
-
Merci!Maintenant,j'ai une compréhension claire duparamètre «vrai»!Thanks! Now I've a clear understanding of the 'true' parameter!
- 0
- 2011-08-06
- Rutwick Gangurde
J'ai untableauenregistré dans lespostmata,chaque clé detableau devient unemétakey.Je veux changer le codepourenregistrer letableauentier avec unmétakey.Commentfaire ça?Merci!