Skip to content

Instantly share code, notes, and snippets.

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/77a7eb708815aa87ccdf25b7fc02f1bc to your computer and use it in GitHub Desktop.
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/
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