1

When sending a comment to the bin, the attached "comment replies" does not get trashed or deleted.

How can you configure wordpress so that when you trash a comment, the comment replies automatically gets deleted?

2

You could build an action for delete_comment, where you loop through the childcomments and delete them.

I use two different functions here, one that trashes the childcomments (hooked into trash_comment), and one that deletes them directly.

Please make sure which funtion you use, or if you want to use both. The safe version would be to move the childcomments to trash, not delete them in the first place - in this case you have to hook f711_trash_child_comments to delete_comment.

Please be aware that this function is fully recursive. The actions get called before the comment is actually deleted, so in a timeline the nested comments get deleted first, from the bottom level up.

add_action( 'delete_comment', 'f711_delete_child_comments' ); // complete deletion
add_action( 'trash_comment', 'f711_trash_child_comments' ); // move to trash


function f711_delete_child_comments( $comment_id ) {

    global $wpdb;
    $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) ); //select the comments where the parentcomment is the comment to be deleted

    if ( !empty($children) ) {
        foreach( $children as $thischild => $childid ) {

            wp_delete_comment( $childid, true ); // set second parameter to false if you just want to move it to the trash

        }

    }

}

function f711_trash_child_comments( $comment_id ) {

    global $wpdb;
    $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) ); //select the comments where the parentcomment is the comment to be deleted

    if ( !empty($children) ) {
        foreach( $children as $thischild => $childid ) {

            wp_trash_comment( $childid ); // set second parameter to false if you just want to move it to the trash

        }

    }

}
2
  • I can't get that code to work. Any ideas? Thanks for the help!
    – dkl
    Feb 1 '14 at 11:43
  • Ok, I got it to work now. But, I don't like that I have to update the back-end user menu when for it to be removed from the comment section. I would like it to be instant. Is this something that can be achieved?
    – dkl
    Feb 1 '14 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.