I want to delete the post attachments too from the 'Media" panel once a custom post is deleted. The post itself gets deleted well, but its media still remains in the wordpress "media" panel:
Archive page:
<?php
if (isset($_POST['delete-btn'])) {
$deletePost = $_POST['post-id'];
wp_delete_post( $deletePost, true);
do_action( 'delete_post', $_POST['post-id'] );
}
?>
Functions.php:
//delete all media attached with deleted post
add_action( 'delete_post', 'delete_post_children' );
function delete_post_children( $post_id )
{
wp_delete_attachment( $post_id );
};
//delete all media attached with deleted post end
Please help. Thanks.
do_action( 'delete_post', $_POST['post-id'] );
is not necessary, try commenting this line and tring again, also do aprint_r
to$_POST
to check that all the properties exist and have the expected valuewp_delete_attachment( $post_id, true )
.