Add_post_meta incorrectly storing arrays
Okay, I’m pulling my hair out here. I’m trying to use add_post_meta and update_post_meta to store some values. Instead of creating a bunch of rows, I’d like to have one row with an array of data in the meta_value field. But, it appears to me that WP core is incorrectly storing these. If I run an add_post_meta on an array of data, it ends up looking like this in the database:
(Note that beginning s:152 – meaning that it’s storing it as a string.)
If I store the same array via add_option(), it correctly stores like so:
Has anyone else encountered this? Are you just “not allowed” to store arrays in the post meta fields?
But both of your strings are, well, strings. That’s kinda what ‘serialize’ does. Do your values not unserialize correctly?
Right. I realize they’re both strings – but the first doesn’t unserialize correctly, while the second one does.
Ok. I just inserted three different arrays with add_post_meta and none of them followed the ‘s:’ pattern. I don’t think it is WP, at least not a default install of WP.
Serialize, in my experience, is squirrelly. So…
1) Try with all of your plugins disabled.
2) What does your array look like? Do
print_r($yourarray);, view source, copy the array, and paste it here if its short or in the pastebin if its long. Do this right before you give the array to add_post_meta.
3) Copy the whole serialized array from the database and paste it here if its short or in the pastebin if its long.
Hm…. You’re right – I tried it on a clean install and no issues. Now to track down the culprit….
Ugh… I ran into this too, but a clean install wasn’t really a feasible option. I ended up just bypassing post meta functions (get_post_meta, add_post_meta, update_post_meta), and instead doing straight queries with the $wpdb object… seems to have done the trick, but I’d really like to know what was causing the strange serialization issues…
update_metadata () function, so that makes it appropriate serialized, update_post_meta () is okay to pass the data is not serialized.
- The topic ‘Add_post_meta incorrectly storing arrays’ is closed to new replies.