使用 update_post_meta 将序列化数据插入 WordPress Post Meta 的正确方法

Correct way to insert serialized data into WordPress Post Meta using update_post_meta

我正在尝试使用 update_post_meta 函数将序列化数据插入 wp_postmeta table。

然而,当我将数据插入 table 时,它正在被更改。例如,这是我要插入的内容:

update_post_meta($feed_id, 'wprss_ftp_taxonomies', 'a:3:{i:0;a:6:{s:8:"taxonomy";s:6:"source";s:5:"terms";a:1:{i:0;s:3:"'.$source_slug.'";}s:4:"auto";s:5:"false";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}i:1;a:6:{s:8:"taxonomy";s:5:"topic";s:5:"terms";s:0:"";s:4:"auto";s:4:"true";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}i:2;a:6:{s:8:"taxonomy";s:10:"categories";s:5:"terms";s:0:"";s:4:"auto";s:5:"false";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}}');

所以我的序列化数据是这样的:

a:3:{i:0;a:6:{s:8:"taxonomy";s:6:"source";s:5:"terms";a:1:{i:0;s:3:"'.$source_slug.'";}s:4:"auto";s:5:"false";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}i:1;a:6:{s:8:"taxonomy";s:5:"topic";s:5:"terms";s:0:"";s:4:"auto";s:4:"true";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}i:2;a:6:{s:8:"taxonomy";s:10:"categories";s:5:"terms";s:0:"";s:4:"auto";s:5:"false";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}}

然而它被插入到数据库中是这样的:

s:577:"a:3:{i:0;a:6:{s:8:"taxonomy";s:6:"source";s:5:"terms";a:1:{i:0;s:3:"abc";}s:4:"auto";s:5:"false";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}i:1;a:6:{s:8:"taxonomy";s:5:"topic";s:5:"terms";s:0:"";s:4:"auto";s:4:"true";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}i:2;a:6:{s:8:"taxonomy";s:10:"categories";s:5:"terms";s:0:"";s:4:"auto";s:5:"false";s:14:"filter_subject";s:0:"";s:15:"filter_keywords";s:0:"";s:28:"post_taxonomy_compare_method";s:3:"all";}}";

它看起来像是将它作为字符串插入,而不是仅仅将值设置为我输入的原始序列化数据。

我不知道如何纠正这个问题,因为我不经常直接处理序列化数据。

如有任何见解,我们将不胜感激。

您可以“反序列化”,这将正常工作

$data   =    unserialize( 'YOUR_CODE_SERIALIZED_HERE' );
update_post_meta($feed_id, 'wprss_ftp_taxonomies', $data);

不要序列化数组。直接赋值给update_post_meta作为数组。我刚刚在这里解决了我自己的问题 -> Why is there a string saved in my serialized array?