Home › Forums › Backend Issues (wp-admin) › How to automatically detach a file from a post (deleted via edit post)? › Reply To: How to automatically detach a file from a post (deleted via edit post)?
You need to create an acf/save_post action that runs at a priority of <10 so that it runs before ACF has updated the DB.
In this action you need to compare the old values with the new values.
The new values will be in a nested array in $_POST[‘acf’]. The old values can be retrieved using a have_rows() loop. Both values will be attachment IDs. You need to collect a list of both the old values and the new values, compare the lists and anything in the old values that is not in the new values you need to update the post_parent of the attachment to set it to 0 (zero).
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!
© 2022 Advanced Custom Fields.