Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tallesairan/026996b687f8ab32aa8c5ac24ee31a90 to your computer and use it in GitHub Desktop.
Save tallesairan/026996b687f8ab32aa8c5ac24ee31a90 to your computer and use it in GitHub Desktop.
Delete WooCommerce products with images (attachment)
<?php
function delete_associated_media( $id ) {
$media = get_children( array(
'post_parent' => $id,
'post_type' => 'attachment'
) );
if( empty( $media ) ) {
return;
}
foreach( $media as $file ) {
wp_delete_attachment( $file->ID );
}
}
add_action( 'before_delete_post', 'delete_associated_media' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment