Jump to content

Wordpress - Trouble Updating Post Meta


joshlevinson

Recommended Posts

My plugin is a custom export/update plugin that lets the user export his products, update the price grids, and upload the csv to change the data quicker than using the visual editor.

The price grids are stored as a serialized array in the post's meta (_cp_array).

 

To test my plugin, I uploaded a csv with data identical to that which was already in the DB. The function below informed that the Table Rates had not changed (because they hadn't!).

 

Next, I changed ONE value in ONE row of the csv. Tested again. This time, the function informed that for the product I changed, the post meta had been updated.

 

However, the meta is never actually updated. Looking in the DB for the meta (_cp_array), the serialized string stays the same after running my upload function multiple times, with update_post_meta returning true each time, and echoing the "updated" message.

 

For debugging purposes, I used var_export($unserialized) to see what it outputted, and also var_export(serialize($unserialized)) to see what the data should look like when writing the serialized array/string to the DB.

 

The var_export (serialized and not) on my plugin screen always displays the new, changed data from the csv.

 

It gets even stranger...next I tried adding new metadata to a product that didn't have anything for that specific metadata (it was empty and the meta key didn't exist for that product). The function displays that it updated the metadata for that product. However, looking in the DB reveals that the serialized value for that product's meta (_cp_array) was very different from the data I entered! The var_export displays the correct array and serialized string, but the DB had very different keys and values!

 

All relevant examples/info: http://pastebin.com/jYjuzg6G

 

Why is the plugin not changing the metadata, even though the function returns true? And why, when it added new metadata, did it add incorrect metadata, even though the serialized array was correct when outputted to the screen?

 

The below code is responsible for updating the product's _cp_array:

if(update_post_meta($id, '_cp_array', $unserialized))
echo 'Table Rate Prices Updated.<br />';
else echo 'No Table Rate Change.';
var_export($unserialized);
echo '<br />';
var_export(serialize($unserialized));

Edited by joshlevinson
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.