0

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.

2
  • I think that this do_action( 'delete_post', $_POST['post-id'] ); is not necessary, try commenting this line and tring again, also do a print_r to $_POST to check that all the properties exist and have the expected value Commented Jul 14, 2021 at 11:47
  • If you want to permanently delete an attachment, try setting the second parameter to true: wp_delete_attachment( $post_id, true ).
    – Sally CJ
    Commented Jul 14, 2021 at 15:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.