如果帖子标题等于“组件”或“着陆页”,我将自定义字段插入postmeta表。
在我的add_post_meta()
函数里面,我在开头用双引号获得带有额外字符串的序列化数据,最后用带分号的双引号。
预计a:5:{s:5:"param";s:4:"post";s:8:"operator";s:2:"==";s:5:"value";i:3309;s:8:"order_no";i:0;s:8:"group_no";i:1;}
目前插入s:110:"a:5:{s:5:"param";s:4:"post";s:8:"operator";s:2:"==";s:5:"value";i:3309;s:8:"order_no";i:0;s:8:"group_no";i:1;}";
这是我的代码:
if ($menupost->post_title == "Components" || $menupost->post_title == "Landing Page") {
global $wpdb;
$posttitle = 'Tablet';
$postid_ofacf = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "' and post_type='acf'");
$post_meta = get_post_meta($postid_ofacf, "rule", false);
$post_count = count($post_meta);
$last_array = $post_meta[$post_count - 1];
$insert_acf_rule_meta = array();
foreach ($last_array as $key => $value) {
if ($key == "group_no") {
$value = $value + 1;
}
if ($key == "value") {
$value = $post_id;
}
$insert_acf_rule_meta[$key] = $value;
}
add_post_meta($postid_ofacf, 'rule', serialize($insert_acf_rule_meta));
$get_acf_post_args = array(
'post_title' => 'Tablet',
'post_status' => 'publish',
'post_type' => 'acf'
);
}
没有必要将序列化数组作为参数传递给add_post_meta
函数,因为如果给出了数组,它会调用serialize
函数,如documentation中所述。
改写这一行:
add_post_meta($postid_ofacf, 'rule', serialize($insert_acf_rule_meta));
至:
add_post_meta($postid_ofacf, 'rule', $insert_acf_rule_meta);
希望这可以帮助。