Skip to content

Instantly share code, notes, and snippets.

@Boztown
Created December 1, 2013 21:05
Show Gist options
  • Save Boztown/7740769 to your computer and use it in GitHub Desktop.
Save Boztown/7740769 to your computer and use it in GitHub Desktop.
Wordpress: Delete attachments after a custom post type is deleted.
function handle_delete_listing( $post_id )
{
$cargs = array(
'post_type' => 'attachment',
'post_parent' => $post_id
);
$attachments = get_children($cargs);
if ($attachments) {
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, true );
}
}
}
add_action( 'before_delete_post', 'handle_delete_listing' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment