Support

Account

Home Forums General Issues Action to check if specific field value changed on post update?

Solved

Action to check if specific field value changed on post update?

  • I’m using WooCommerce and trying to create a custom email notification when an acf text field value changes. In order to send the email, I have to create a trigger, so I’m looking for an action that will fire, or some way to compare old/new values if this field value changes when the Save Order button on the order page is clicked and the order is updated.

    I saw this post from 2 years ago, but I’m not sure how to use acf/load, or acf/save_post for that matter, to save the value into a variable, and I was also hoping there might be an easier way to do this now..

    Any ideas would be very much appreciated.

  • If you use and acf/update_value/ filter then the old value is still in the database and the new value is passed to the filter. You can use get_post_meta() to get the old value and compare it to the new value. I’m not sure you can use get_field() here because accessing a field while it’s being saved may have side effects, but you could try it.

    
    add_filter('acf/update_value/name=my_field_name', 'my_check_for_change', 10, 3);
    function my_check_for_change($value, $post_id, $field) {
      $old_value = get_post_meta($post_id, $field['name'], true);
      if ($old_value != $value) {
        // it changed
      }
      return $value;
    }
    
  • Thanks for your quick reply! Your example worked to compare the values, but the $old_value is returning an array rather than a single value. This is the output from print_r($field):

    Array ( [key] => field_56607351f4d8b [label] => Delivery Method [name] => delivery_method [_name] => delivery_method [type] => text [order_no] => 1 [instructions] => [required] => 0 [id] => acf-field-delivery_method [class] => text [conditional_logic] => Array ( [status] => 0 [allorany] => all [rules] => 0 ) [default_value] => [allow_null] => 0 [multiple] => 0 [formatting] => html [maxlength] => [placeholder] => [prepend] => [append] => ) 1

    I’m not sure the difference between [name] and [_name] but perhaps since they have the same value, that’s why I’m getting an array?

  • for some reason it’s getting the field object, which is odd, and I’m not sure right now why. Try replacing field['name'] with your field name

    
    add_filter('acf/update_value/name=my_field_name', 'my_check_for_change', 10, 3);
    function my_check_for_change($value, $post_id, $field) {
      $old_value = get_post_meta($post_id, 'my_field_name', true);
      if ($old_value != $value) {
        // it changed
      }
      return $value;
    }
    
  • Perfect! Thanks very much for your help

    • mikode

    • December 4, 2019 at 1:02 am

    How might this be adapted to see if a user profile ACF field has been updated?

    I’ve used it as it currently is but it comes back that it has changed every time even if the field hasn’t.

    • mikode

    • December 4, 2019 at 1:33 am

    Also, its a repeater field that I’m trying to see if it has been updated.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.