Created
February 25, 2023 11:42
-
-
Save dani-snippets/e19dff4206b829e2d7362684ef5467ee to your computer and use it in GitHub Desktop.
delete attached PDF files if a custom-post-type post is permanantly deleted [WordPress]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment