Someone have a sample snippet to delete media ID on deleting post?
i have upload metafield that contain array ID of media file attached to post and i wish to delete media files also on deleting post and avoid to have pending/useless media loaded.
thanks
2
Answers
i'd solved
You would be using the
delete_post
hook, which fires right before a post gets deleted (so you still have access to it). Inside your function, which then fires, you’d fetch all attachments (or attachment IDs) and remove them usingwp_delete_attachment
.References: Hook Function