Add_post_meta incorrectly storing arrays (7 posts)

  1. DeannaS
    Posted 5 years ago #

    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:
    s:152:"a:4:{s:9:"blog_Name";s:9:"Test Blog";s:8:"b...

    (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:
    a:4:{s:9:"blog_Name";s:9:"Test Blog";s:8:"blog_url.

    Has anyone else encountered this? Are you just "not allowed" to store arrays in the post meta fields?

  2. s_ha_dum
    Posted 5 years ago #

    But both of your strings are, well, strings. That's kinda what 'serialize' does. Do your values not unserialize correctly?

  3. DeannaS
    Posted 5 years ago #

    Right. I realize they're both strings - but the first doesn't unserialize correctly, while the second one does.

  4. s_ha_dum
    Posted 5 years ago #

    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.

  5. DeannaS
    Posted 5 years ago #

    Hm.... You're right - I tried it on a clean install and no issues. Now to track down the culprit....

  6. groovenectar
    Posted 5 years ago #

    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...

  7. gfb00756@yahoo.co.jp
    Posted 4 years ago #

    update_metadata () function, so that makes it appropriate serialized, update_post_meta () is okay to pass the data is not serialized.


Topic Closed

This topic has been closed to new replies.

About this Topic