I’m using WordPress update_post_meta to save an array like
$obj = array(
'array' => array(1, 'zwei', !!3),
'string' => 'abc',
'bool' => true,
'bool2' => false,
'integer' => 1,
'integer2' => 17
);
update_post_meta($post_ID, 'my-key', $obj);
however if I check the raw field i get
a:6:{s:5:"array";a:3:{i:0;i:1;i:1;s:4:"zwei";i:2;s:1:"1";}s:6:"string";s:3:"abc";s:4:"bool";s:1:"1";s:5:"bool2";s:1:"0";s:7:"integer";i:1;s:8:"integer2";i:17;}
while it should be
a:6:{s:5:"array";a:3:{i:0;i:1;i:1;s:4:"zwei";i:2;b:1;}s:6:"string";s:3:"abc";s:4:"bool";b:1;s:5:"bool2";b:0;s:7:"integer";i:1;s:8:"integer2";i:17;}
You may notice that all boolean are stored as string (b:1 = s:1:"1"
)
The problem is only on certain WordPress installations and not on every one. I’ve also checked the serialize function which is working correct (returns b:1
)
Also using get_post_meta
get_post_meta($post_ID, 'my-key', true);
and checkin the value with is_bool returns false (obviously)
EDIT: just noticed also integers get saved as strings
update_post_meta uses update_metadata you can find it’s code here in that function if you have a look at line 119 you will see that the meta_value is passed to wp_unslash which return value is a string (the function that changes the data type is stripslashes_deep)
As a workaround you can serialize the value before passing it to update_post_meta()
EDIT:
Found the issue: before 3.6.0 update_metadata had on line 117 this code:
stripslashes is a php function that returns a string.
as of 3.6.0 that line looks like:
which stripslashes only string type meta_keys.
An update to wordpress 3.6.0 or above will fix the issue.