Support Forum
Support › MB Frontend Submission › Delete Media Files When Delete
I created a custom post type that accepts a video upload and an image upload.
In the database they are displayed as the custom field name and then the URL of the file. They are not attached.
I noticed on a normal post the file is listed as _wp_attached_file.
The question is... how do I delete the uploaded media when I delete the custom post type entry?
I tried several of the suggestions listed here, but they are assuming that the files are attached.
Any ideas?
Or is there a different way to upload the video and image that I am using?
Currently I am using file_input for both the video and the image.
I changed the field to file_upload and the video and the image is still not attached.
I am using this via the Frontend Submission plugin if that helps.
Here's a sample of my metabox.
$meta_boxes[] = [
'title' => 'Video Information',
'post_types' => 'ctavideos',
'id' => 'ctavideos',
'hidden' => array(
array( 'ctavideos_kind_of_player_select', '!=', 'standard' ),
array( 'ctavideos_kind_of_player_select', '!=', 'ctaplayer' ),
),
'class' => 'fieldwrapper',
'columns' => 12,
'fields' => [
[
'type' => 'heading',
'name' => 'Video Details',
'desc' => 'Select your video and upload it to our system. Your video should be in an mp4 format in order to display correctly. If it is not in an mp4 then you must convert it first.',
],
[
//'name' => 'Video Upload',
'placeholder' => 'Click the "Select" button to upload your video.',
'id' => 'ctavideos_video',
'type' => 'file_upload',
'columns' => 11,
],
[
//'name' => 'Poster Image',
'placeholder' => 'Click the "Select" button below to upload an image.',
'id' => 'ctavideos_posterimage',
'type' => 'file_upload',
'image_size' => 'medium',
'columns' => 11,
],
]
];
I added force_delete to my fields and same result.
I did some more testing on various upload fields. Most are not working correctly. They are not attaching themselves to the post.
Here are my results...
Field Type Attached/Unattached
image - Attached
video - Unattached
file - Attached
file_input - Unattached
file_advanced - Unattached
file_upload - Unattached
The ones mark Unattached are not attaching themselves to the post. I change the above metabox information and only changed the fields and then looked into the media library to see if the video or image is attached or not.
Update... I went through same tests, this time with the Frontend Submissions Plugins installed rather than using it through. Metabox AIO. The results where still the same.
The only two upload fields that work properly are... file and image uploads. These two the media upload is attached to the post in the Media Library.
The other upload fields that I have listed above.. DO NOT attach to the post and are listed as "Unattached".
Hi,
Meta Box and WordPress do not support deleting field values (post meta) and images uploaded when deleting the post. You can refer to this topic to use a plugin
https://support.metabox.io/topic/remove-unused-images-from-media-library/
or this article to use the code to delete images https://www.isitwp.com/remove-featured-image-deleting-post/
I am currently using similar code to this article you posted... https://www.isitwp.com/remove-featured-image-deleting-post/, modified to remove custom post type attachments upon deletion.
The issue is this...
1.) In my custom post type, I am uploading an image and a video along with other information.
2.) When I look in the Media Library the image and the video uploaded is not attached to any post it is shown as "Unattached".
3.) The only time this does not happen is if I change the fields I am using to the "image" field or the "file" field. When using either of these fields the files will show in the Media Library as "Attached".
So even using code such as you suggest in the link above, if I am using fields such as "video", "file_input", "file_advanced" or "file_upload" the code will not work because the if you use any of these fields, the file will upload as "Unattached" in the media library.
Shouldn't using any of these fields result in the media being attached to that specific post?
See my point?
Hi,
I'm using the field single_image
, and with this code, when you delete the post, the attachment will also be deleted.
add_action( 'before_delete_post', 'remove_attachment_with_post', 10 );
function remove_attachment_with_post( $post_id ) {
$image_id = get_post_meta( $post_id, 'single_image', true); //1691 image ID
if( $image_id ) {
wp_delete_attachment( $image_id, true );
}
}
If you use a field with the setting 'multiple' => true
like file_advanced
, you will need to create a loop to iterate through an array of image IDs. Read more on the documentation https://docs.metabox.io/fields/file-advanced/#template-usage
Hope that makes sense.
Ok, after a HUGE amount of testing I figured it out exactly what the problem is with the media attachment issue.
I created a metabox with every option of upload. I tested with an image and a video.
My first test results are here...
https://ibb.co/C7BXYCS
As you can see image_input (shown as select by mistake), file_input, file_advanced, image_advanced, single_image, and video are shown as UNATTACHED.
Seeing your results were not the same as mine, I tried to figure out what was different. The difference is, I was using a Custom Post Type and you were either using a metabox on a "post" or "page".
So I changed all metaboxes in my test to display on a "page" and I reran all the tests again.
Here are my second results...
https://ibb.co/Qns93NQ
As you can now see, when the same fields are uploaded via a "page" metabox everything works perfectly as it should and every upload is attached to the post as it should be.
So the problem is image_input, file_input, file_advanced, image_advanced and single_image fields DO NOT attach to the post if you are using a custom post type.
So this is what needs to be fixed.
I hope this makes sense.
Hi,
Just focus on your main question:
The question is... how do I delete the uploaded media when I delete the custom post type entry?
Then using the custom code above will help you to delete the uploaded media when deleting a post/custom post type.
https://support.metabox.io/topic/delete-media-files-when-delete/#post-33805
"Attach" or "Unattached" status on the Media page does not relate to the code above.
On the other hand, the post attached on the Media list is just used another post meta with the meta key _wp_attached_file
and works as a custom field. See here
https://monosnap.com/file/VUeK4M9fuwRHUMsSZ5eh7ySYWIuqVy
https://monosnap.com/file/i1uEjwEDD3bf9W3FpPJX6629AMAgRi
Each image uploaded will be stored in a post with the post type "Attachment", screenshot https://monosnap.com/file/zzgI45SYVflMmXi73tDFgArKulyt6n
Maybe you are confusing this post type with the "Attach" action on the Media page.
Thank you for your responses. I do appreciate them, but we are not quite understanding each other as to what the issue is.
I've made a video showing you the difference in the database.
https://www.youtube.com/watch?v=bjun2ljc4IA