3

Got an issue when inserting a blank serialized array into the database. I'm using wordpress (just for full disclosure, shouldn't make a difference) and using the add_post_meta() function to input some additional information. Here's a snippet:

add_post_meta($post_id, 'information', serialize(array()));

The serialized array is intentionally blank for the moment. Anyway here's the expected/actuals:

// Expected value 
a:0:{}

// Actual value
s:6:"a:0:{}";

Any help?

2
  • Try reading this codex.wordpress.org/Function_Reference/add_post_meta Commented Aug 2, 2013 at 4:22
  • 1
    Your s:6 comes from this - a:0:{}, a string that is 6 characters long. I believe you don't need to serialize array data with add_post_meta(). You could also try maybe_serialize();. Commented Aug 2, 2013 at 8:25

2 Answers 2

1

Add post meta auto-serializes arrays. And also get post meta, pass TRUE at the last argument: like get_post_meta($post_id, 'information', TRUE);

More details here: http://codex.wordpress.org/Function_Reference/add_post_meta

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. It seems that you don't need to serialize the data for add_post_meta as it does it for you if you input an array. Therefore my problem was that my data was double serialized.
0

I have faced the same issue but I could used add_post_meta($post_id, 'information', array()); instead of add_post_meta($post_id, 'information', serialize(array())); try it you can resolve your issue..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.