Support

Account

Home Forums Backend Issues (wp-admin) How to automatically detach a file from a post (deleted via edit post)?

Solved

How to automatically detach a file from a post (deleted via edit post)?

  • Someone figured out how to detach a file from a post when it is deleted from the post (file in a repeater field)?

    I am offering the client a file field within a repeater. So the client can edit a post (CPT) and upload a file to the post in the backend. All the files uploaded to the post will be automatically shown as a list in the frontend (PDF download list).

    If the client deletes the uploaded file from the edit post screen, it’ll still show in the frontend as it is still detached to the post.

    Workaround: go to the media library → search for the file → detach from post

    The issue is described from someone else here as well
    https://support.advancedcustomfields.com/forums/topic/removing-attachment-post_parent-on-file-field-type/

    I am working with ACF Pro and Dynamic.ooo to show the files in the frontend.

    That sounds like a valid question to me so I wonder if anyone ever found a solution for this? Wouldn’t it be logical to automatically detach the file from the 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).

  • If you can get the attachment ID, you might be able to use:
    wp_delete_attachment( $attachment->ID, 'true' );

  • Thanks to both of you! I am not there yet to get too deep into code myself yet. I thought that this is a “basic” request and that its quite logical to provide this functionality out of the box.

    Just makes sense to me, but perhaps I do not consider all use cases.

    For now, I teach the client how to detach an attachment if needed. Perhaps I can get into it again in the future or a global setting will be developed for such cases.

  • Having this done automatically would be like saying that if an image is removed from “The Content” of a post that WP should automatically detach the image from the post or delete the image.

    All images that are uploaded while editing a post are “attached” to that post.

    In addition to this, if an image is uploaded while editing a different post then it is attached to that post. Selecting or inserting that image on a second post does not change or add that “attachment”. An image can only be attached to a single post and this attachment will never be changed automatically when selecting previously uploaded images.

    Showing images or files based on what post they are attached to is prone to issues and you would be better off in the long run by showing a list of files that are actually current in the repeater.

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

You must be logged in to reply to this topic.