Convert comma separated list to serialized array to import as post meta

I have a very site WordPress site that was custom built with custom tables for a membership system.I'm updating t

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
Add a comment  | 

1 Answer 1

Reset to default 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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745116095a4612135.html

相关推荐

  • Convert comma separated list to serialized array to import as post meta

    I have a very site WordPress site that was custom built with custom tables for a membership system.I'm updating t

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信