I'm looking to insert this serialized value into a meta_value in the wp_postmeta table


I've tried numerous things

Such as creating the associative array manually.

$defaultquantityarray =  array('quantity' => '2');
$defaultquantity = serialize($defaultquantityarray);

update_post_meta( $post_id, '_default_attributes', $defaultquantity);

I've also referenced the update_post_meta documentation and found reference to something like this

$escaped_json = 'a:1:{s:8:"quantity";s:1:"2";}';

update_post_meta( $post_id, '_default_attributes', wp_slash( $escaped_json ));

and additionally with a:1:.

Both result in this insert into my meta_value row


This is all being performed in a WP_Query loop.

How do I insert a meta_value of a:1:{s:8:"quantity";s:1:"2";} into my _default_attributes meta_key?

1 Answer 1


Wow okay, so after posting this I almost immediately came up with the solution.

It was a combination of the two things I tried.

$defaultquantityarray =  array(
 'quantity' => '2'

update_post_meta( $post_id, '_default_attributes', $defaultquantityarray);

Apparently WordPress automatically serializes data in some meta_values, and I was essentially serializing them twice hence the s:29: prefix.

  • arrays and objects get automatically serialized/unserialized because they are php data types being stored as strings by MySQL.
    – Milo
    Commented Jan 5, 2018 at 4:10
  • Actually really interesting, and makes sense. Thanks for the insight
    – bbruman
    Commented Jan 5, 2018 at 4:14
  • You should sanitize the input data before sending it to database. Please, refer to the link for details about WordPress sensitization methods codex.wordpress.org/…
    – BlueSuiter
    Commented Jan 5, 2018 at 4:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.