Created
October 22, 2018 02:33
-
-
Save marcelotorres/dace1ac4b74bcd8fee4de0689d1a8267 to your computer and use it in GitHub Desktop.
Deletar anexos automaticamente ao deletar um post no WordPress: https://www.marcelotorresweb.com/deletar-anexos-automaticamente-ao-deletar-um-post-no-wordpress/
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
<?php | |
function delete_associated_post( $post_id ) { | |
if( get_post_type( $post_id ) === 'books' ){ | |
$medias = get_children( array( | |
'post_parent' => $post_id, | |
'post_type' => 'attachment' | |
) ); | |
if( $medias ){ | |
foreach( $medias as $media ) { | |
wp_delete_attachment( $media->ID ); | |
} | |
} | |
} | |
} | |
add_action( 'before_delete_post', 'delete_associated_post' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment