I have this code:

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

}

I want to compare dates and add dates or remove dates from another tables based on from- and to-date.

The issue is that from- date and to-date can be totally different depending in which order the hooks acf/update_value/name=bookitall_fromdate and acf/update_value/name=bookitall_todate occurs. I want to do the comparision when both datefields (bookitall_fromdate and bookitall_todate) has been updated.

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.

ACF launches these hooks on the hook save_post then you can compare values just before like that :

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
  • hmm you probably want a later priority not an earlier one – Mark Kaplun Aug 10 at 7:09
  • with a later priority, the old values are probably lost, erased by new values. – Kaperto Aug 10 at 7:48
  • maybe we understood the question differently. My understanding is that he wants to compare them after they were both updated – Mark Kaplun Aug 10 at 8:23
  • @MarkKaplun - You are correct - I want to compare values AFTER update. – bestprogrammerintheworld Aug 10 at 19:30

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.