Reputation: 3267
When i try to execute this code
update_post_meta( $id, "_woocommerce_my_meta", 'a:1:{s:4:"gtin";s:13:"10";}' );
The meta was changing to
s:27:"a:1:{s:4:"gtin";s:13:"10";}";
By simply changing the code to
update_post_meta( $id, "_woocommerce_my_meta", ':a:1:{s:4:"gtin";s:13:"10";}' );
adding a ":" at the end of string, it works...
But i dont need this ":" Is this a function bug? Or is there any reason for that? Or some way around this problem?
Upvotes: 0
Views: 351
Reputation:
It is not a bug. WordPress has a reason for doing this. Since, WordPress automatically serializes objects and arrays it has to differentiate between a database meta_value that is a serialization and a meta_value that really is just a string that looks like a serialization. To understand this note that update_post_meta() calls maybe_serialize()
function maybe_serialize( $data ) {
if ( is_array( $data ) || is_object( $data ) )
return serialize( $data );
// Double serialization is required for backward compatibility.
// See https://core.trac.wordpress.org/ticket/12930
// Also the world will end. See WP 3.6.1.
if ( is_serialized( $data, false ) )
return serialize( $data );
return $data;
}
Note that if the meta value is a serialized string then it is serialized again.
update_post_meta() and get_post_meta() automatically handles serialization and deserialization for objects and arrays. Are you sure you need to use a serialized value in your call to update_post_meta()? Note that get_post_meta() will deserialize the serialized string and return the original string.
Upvotes: 1