您可以为delete_comment
, 在这里,您可以循环浏览childcomments并删除它们。
我在这里使用了两个不同的函数,一个是垃圾childcomments(连接到trash_comment
), 还有一个直接删除它们的。
请确定您使用哪种功能,或者如果您想同时使用这两种功能。安全的版本是将childcomments移动到垃圾箱,而不是首先删除它们-在这种情况下,您必须钩住f711_trash_child_comments
到delete_comment
.
请注意,此函数是完全递归的。调用操作before 注释实际上已被删除,因此在时间轴中,嵌套的注释首先从最底层开始删除。
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
}
}
}