我有以下代码:
public function init() {
add_filter(\'acf/update_value/name=bookitall_fromdate\',
array($this, \'check_bookingdates\' ), 10, 3);
add_filter(\'acf/update_value/name=bookitall_todate\',
array($this, \'check_bookingdates\' ), 10, 3);
}
public function check_bookingdates($new_value, $post_id, $field) {
$post_type = get_post_type( $post_id );
if ( $post_type != \'bookitall_bookings\' )
{
return;
}
$old_value = get_post_meta($post_id, $field[\'name\'], true);
if ( $old_value == $new_value ) { //No change
return $new_value;
}
...code to check (comparision) fromdate and to date
}
我想比较日期,并根据开始日期和结束日期从其他表中添加日期或删除日期。
问题是,起始日期和截止日期可能完全不同,这取决于挂钩的顺序acf/update_value/name=bookitall_fromdate
和acf/update_value/name=bookitall_todate
发生。我想在两个日期字段(bookitall_fromdate
和bookitall_todate
) 已更新。
How do I achieve that?
This doesn\'t have to be with acf-fields. The same question applies when using relevant update_meta_key in a filter.
SO网友:Kaperto
ACF在钩子上启动这些钩子save_post
然后您可以像这样比较前面的值:
add_action("save_post", function ($post_ID, \\WP_Post $post, bool $update) {
// read old values with get_post_meta($post_id, "bookitall_fromdate", TRUE) ...
// read new values in $_POST
// do action if the 2 dates are changed
}, 6, 3); // priority 6 for launching before ACF saving with priority 10