Skip to content

Instantly share code, notes, and snippets.

@mattneal-stafflink
Created October 6, 2021 10:03
Show Gist options
  • Save mattneal-stafflink/8e461b805b3ddc2810a3eab76cf89fe7 to your computer and use it in GitHub Desktop.
Save mattneal-stafflink/8e461b805b3ddc2810a3eab76cf89fe7 to your computer and use it in GitHub Desktop.
Delete attached media when deleting custom post type called "Property".
add_action( 'before_delete_post', 'delete_all_attached_media' );
function delete_all_attached_media( $post_id ) {
if( get_post_type($post_id) == "property" ) {
$attachments = get_attached_media( '', $post_id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment