Created
October 22, 2018 02:30
-
-
Save marcelotorres/77a7eb708815aa87ccdf25b7fc02f1bc to your computer and use it in GitHub Desktop.
Por padrão o WordPress não exclui os anexos(imagens, PDF...) de um post quando você exclui o post, ao invés ... https://www.marcelotorresweb.com/deletar-anexos-automaticamente-ao-deletar-um-post-no-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
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