简体   繁体   中英

Storing Json value in add_post_meta

How can I store this Json value in add_post_meta

  $offer_name= $item['data'][$i]['Offer']['name'];

  add_post_meta($post_id, 'offer_name', $offer_name);

The json value of $offer_name doesn't get stored but normal string get stored.

Update 1: this is how I tested .

case 1 // With json the value doesn't get stored..

 $offer_name= $item['data'][$i]['Offer']['name']; 

echo $offer_name; // the value gets echoed .

 add_post_meta($post_id, 'offer_name', $offer_name); // the value of $offer_name doesn't  get stored 

case 2 // without json the value get stored..

  $offer_name="My  Name"; 

 add_post_meta($post_id, 'offer_name', $offer_name); // the value of $offer_name does get stored 

Update 2: if i store the json data in "{ }" then the value get stored in WordPress

$offer_name ="{$item['data'][$i]['Offer']['name']}";


add_post_meta($post_id, 'offer_name', $offer_name);

Thanks in Advance

You need to use json_encode() . This will return a JSON formatted string which you can use to save in your database.

Note that with characters such as ë, â, Æ etc you should use addslashes() on your JSON encoded string before you attempt to save it in the database. This way once you decode the JSON string these special characters can be seen properly.

(Yes this is very old but I am searching for something myself and thought why not eh?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM