我正在尝试获取一些自定义字段元数据(通过ACF插件生成),并将其保存为带有函数的post meta。这就是我想到的:
function tps_create_slotcode($post_id) {
if (get_post_type($post_id) == \'space-rental\') {
$slotCodes = array();
while (have_rows(\'dates_and_times\', $post_id)) {
the_row();
$date = get_sub_field(\'date\');
$dateStamp = strtotime($date);
$start = get_sub_field(\'start_time\');
$end = get_sub_field(\'end_time\');
$space = get_sub_field(\'space\'); //term id
$spaceTerm = get_term($space, \'spaces\');
$slotCodes[] = $spaceTerm->slug.\'_\'.$start.\'_\'.$date;
}
update_post_meta($post_id, \'slotCodes\', $slotCodes);
}
}
add_action(\'acf/save_post\', \'tps_create_slotcode\', 40);
我已经测试了所有变量,以确保它们产生了一个值,而且它们确实产生了一个值,但是update\\u post\\u meta调用没有做任何事情。
如果我将update\\u post\\u meta放在循环中,它会工作,但只保存最后的结果(显然),因为这是循环的最后一次迭代。这就是代码:
function tps_create_slotcode($post_id) {
if (get_post_type($post_id) == \'space-rental\') {
while (have_rows(\'dates_and_times\', $post_id)) {
the_row();
$date = get_sub_field(\'date\');
$dateStamp = strtotime($date);
$start = get_sub_field(\'start_time\');
$end = get_sub_field(\'end_time\');
$space = get_sub_field(\'space\'); //term id
$spaceTerm = get_term($space, \'spaces\');
$slotCodes = $spaceTerm->slug.\'_\'.$start.\'_\'.$date;
update_post_meta($post_id, \'slotCodes\', $slotCodes);
}
update_post_meta($post_id, \'slotCodes\', $slotCodes);
}
}
add_action(\'acf/save_post\', \'tps_create_slotcode\', 40);
有人能告诉我第一个例子中我做错了什么吗?update\\u post\\u meta是否不会保存数据数组或其他内容?