Created
October 6, 2021 10:03
-
-
Save mattneal-stafflink/8e461b805b3ddc2810a3eab76cf89fe7 to your computer and use it in GitHub Desktop.
Delete attached media when deleting custom post type called "Property".
This file contains 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_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