Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Delete WooCommerce products with images (attachment)
function delete_associated_media( $id ) {
$media = get_children( array(
'post_parent' => $id,
'post_type' => 'attachment'
) );
if( empty( $media ) ) {
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
You can’t perform that action at this time.