I got this as an O/P:
s:287:"a:3:{s:6:"actors";a:2:{s:4:"name";s:6:"Actors";s:5:"value";s:38:"Felicity Jones, Diego Luna, Alan Tudyk";}s:8:"director";a:2:{s:4:"name";s:8:"Director";s:5:"value";s:14:"Gareth Edwards";}s:6:"writer";a:2:{s:4:"name";s:6:"Writer";s:5:"value";s:36:"Chris Weitz, Tony Gilroy, John Knoll";}}";
My Code:-
<?php
$arr = array("actors"=>array("name"=>"Actors","value"=>"Curt Clendenin, Michael Ornelas, Keaton Shyler, David Uchansky"),"director"=>array("name"=>"Director","value"=>"Colin Fleming, John Garside"),"writer"=>array("name"=>"Writer","value"=>"Colin Fleming (story), John Garside (story), Jerry Renek (story)"));
$sarr = serialize($arr);
add_post_meta(1,'_attributes',$sarr);
?>
I don't need first s:287" but this thing automatically save in my database. Help me please..
CodePudding user response:
The WordPress function add_post_meta() tries to serialize the passed data for itself.
$arr = [
'actors' => [
'name' => 'Actors',
'value' => 'Curt Clendenin, Michael Ornelas, Keaton Shyler, David Uchansky',
],
'director' => [
'name' => 'Director',
'value' => 'Colin Fleming, John Garside',
],
'writer' => [
'name' => 'Writer',
'value' => 'Colin Fleming (story), John Garside (story), Jerry Renek (story)',
],
];
// just store it as it is - wordpress does the rest
add_post_meta(1, '_attributes', $sarr);
As you can see in the documentation of add_post_meta()
it calls internally the add_metadata() function. This function checks for data, that has to be serialized by calling maybe_serialize(). For BC reasons it doubly serializes already serialzed strings as mentioned in the comments of the function.
Conclusion: Just pass the unserialzed array to the add_post_meta()
function.