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.
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.
Share Improve this question asked Oct 1, 2019 at 7:21 rudtekrudtek 6,3835 gold badges30 silver badges52 bronze badges 1- How are you importing the data? How do you want to do this conversion? With PHP? Or manually? – Jacob Peattie Commented Oct 1, 2019 at 7:53
1 Answer
Reset to default 1If 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 );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745116095a4612135.html
评论列表(0条)