Home › Forums › General Issues › Beer + Location: Tracking Relationship Field Add/Removes? › Reply To: Beer + Location: Tracking Relationship Field Add/Removes?
I don’t understand what kind of notification you want to display/trigger, but you could use the update_value filter to check the difference before saving the new values and notify the way you want on removed/added differences.
function relation_field_update( $value, $post_id, $field ) {
if (wp_is_post_revision( $post_id )) return $value;
$old_values = get_field($field['name']);
$added = @array_diff($value, $old_values);
$removed = @array_diff($old_values, $value);
return $value;
}
add_filter('acf/update_value/name=relation_field', 'relation_field_update', 10, 3);
Where name=relation_field
in the filter is the name of the ACF field you want to verify.
The wp_is_post_revision check is necessary since you don’t want to verify post revision. The @
are there to bypass the exception threw when one of the array is null (this could be done better with condition, e.g. is_array()).
So, $added
will be (empty or) containing the added IDs and $removed
will be (empty or) containing the removed IDs.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.