Skip to content

Instantly share code, notes, and snippets.

@khoipro
Last active January 6, 2024 19:21
Show Gist options
  • Save khoipro/b18372fce7716f052b27e84277e48044 to your computer and use it in GitHub Desktop.
Save khoipro/b18372fce7716f052b27e84277e48044 to your computer and use it in GitHub Desktop.
Sample function delete related attachments (created by ACF) when deleting a post
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 189
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 190
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 191
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 192
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 193
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 194
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 195
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 196
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete gallery attachment id 197
[06-Jan-2024 16:29:06 UTC] INFO: Delete product 188: delete video attachment id 235
[06-Jan-2024 16:29:06 UTC] INFO: No attachment found in database
<?php
/**
* Case is very useful if you have media/file field and set it 'Uploaded to post' feature only
* Disclarm: You should be backup before running a script. I just place and test with sample from one of our projects.
* Steps: (1) Delete a post. (2) Find post in Trash and delete it permanently
*
* @package codetot-labs
* @author codetot
* @since 0.0.2
**
**/
add_action('before_delete_post', 'codetot_delete_post_attachments');
function codetot_delete_post_attachments($post_id) {
global $wpdb;
$post_type = get_post_type( $post_id );
if ($post_type !== 'event') { // Replace with your own post_type key
return;
}
// Delete galley ids
$gallery_ids = get_field('gallery', $post_id);
if ( !empty($gallery_ids) ) {
foreach ($gallery_ids as $gallery_image_id) {
error_log('INFO: Delete post ' . $post_id . ': delete gallery attachment id ' . $gallery_image_id );
wp_delete_attachment($gallery_image_id, true);
}
delete_field('gallery', $post_id);
}
// Delete video id
$video_id = get_field('video', $post_id);
if ( !empty($video_id) ) {
error_log('INFO: Delete post ' . $post_id . ': delete video attachment id ' . $video_id );
wp_delete_attachment($video_id, true);
delete_field('video', $post_id);
}
$attachment_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'");
foreach ( $attachment_ids as $attachment_id ) {
error_log('INFO: Delete post ' . $post_id . ': delete attachment id ' . $attachment_id );
wp_delete_attachment($attachment_id, true);
}
$post_thumbnail_id = has_post_thumbnail( $post_id ) ? get_post_thumbnail_id( $post_id ) : null;
if ( !empty( $post_thumbnail_id ) ) {
error_log('INFO: Delete product ' . $post_id . ': delete post thumbnail attachment id ' . $post_thumbnail_id );
wp_delete_attachment($post_thumbnail_id, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment