0

I have a very site WordPress site that was custom built with custom tables for a membership system. I'm updating the site and trying to figure out how to re-upload the new fields as serialized data.

For instance a "member" CPT has a custom field called "hours_of_operation". The data in that field is "Morning/Daytime". but I would like to get it to something like a:2{{i:0;s:7:"Morning";i:1;s:7:"Daytime";}.

Other data is stored in other fashions, but I'm trying to piece a few together.

  • How are you importing the data? How do you want to do this conversion? With PHP? Or manually? – Jacob Peattie Oct 1 at 7:53
1

If you need to do this conversion in PHP, as part of whatever import process, then you can just convert the comma-separated values into an array with explode(), and then use update_post_meta(), which will automatically serialise the value for you:

$value = 'Morning,Daytime';
$array = explode( ',', $value );

update_post_meta( $post_id, 'hours_of_operation', $array );

Use trim() if your comma separated values include, or could include, spaces:

$value = 'Morning, Daytime';
$array = explode( ',', $value );
$array = array_map( 'trim', $array );

update_post_meta( $post_id, 'hours_of_operation', $array );

If, for whatever reason, you need to serialise the value yuorself in PHP, you can use serialize() to convert the array to a serialised string.

$value      = 'Morning,Daytime';
$array      = explode( ',', $value );
$serialized = serialize( $array );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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