Skip to content

Instantly share code, notes, and snippets.

@marcelotorres
Created October 22, 2018 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelotorres/dace1ac4b74bcd8fee4de0689d1a8267 to your computer and use it in GitHub Desktop.
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/
<?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