Skip to content

Instantly share code, notes, and snippets.

View dani-snippets's full-sized avatar

danielam dani-snippets

  • Germany
View GitHub Profile
@dani-snippets
dani-snippets / delete_attached_media_pdf.php
Created February 25, 2023 11:42
delete attached PDF files if a custom-post-type post is permanantly deleted [WordPress]
add_action( 'before_delete_post', 'delete_attached_media_pdf' );
function delete_attached_media_pdf( $post_id ) {
if( get_post_type($post_id) == "type-your-custom-post-type-name-here" ) {
$attachments = get_attached_media( 'application/pdf', $post_id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}
}
}