Reputation: 891
I want to accomplice the following. I want to automatically send an email on post publish/update when a certain post field value has changed.
[ 'draft', 'ready for group1', 'ready for group 2', 'ready']
I think I need to know 2 things for this. - How and where (what action) do I need to insert custom code on post publish/update - How to compare new post data to old state (and is this possible/availible in the action above)
Upvotes: 0
Views: 702
Reputation: 1317
You can hook onto acf/save_post
for this purpose. Read the documentation here:
https://www.advancedcustomfields.com/resources/acf-save_post/
Since you want the callback to fire before the values are stored, in order to compare the old value against the new one, keep in mind to add a priority of less than 10. Assuming the field with 4 options has the field key field_4afd4af14415f
:
function on_acf_post_save($post_id) {
$post_type = get_post_type($post_id);
if ($post_type === 'your-post-type') {
$old_val = get_field('field_4afd4af14415f', $post_id);
$new_val = $_POST['acf']['field_4afd4af14415f'];
if ($old_val != $new_val) {
// Send desired mail in here:
// wp_mail(...);
}
}
}
add_action('acf/save_post', 'on_acf_post_save', 5, 1); // priority = 5
If your ACF field isn't at the top level, but inside a Group or Repeater, you will have to adapt the code reading from the $_POST['acf']
and get_field()
result.
Upvotes: 1