Skip to content

Instantly share code, notes, and snippets.

@trueqap
Created January 23, 2018 11:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trueqap/fe5c92a7b6d9b8625636c6c09f0e0113 to your computer and use it in GitHub Desktop.
Save trueqap/fe5c92a7b6d9b8625636c6c09f0e0113 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