until the end of the code where wp_set_comment_status() is called. This means that if there is an error or exception before that point, the comment will not be trashed and will remain in the database. To avoid this, you can add some error handling code to make sure that the comment is always trashed even if there is an error. Here's an updated version of the code with error handling:

$comment_id = $_POST['comment_id'];

$qq_rate = get_comment_meta( $comment_id, 'qq_rate', 'true' ); $comment_id_data = get_comment( $comment_id ); $pid = $comment_id_data->comment_post_ID ;

$args = array( 'post_type' => 'qqproduct', 'post_id' => $pid, 'status' => 'approve', );

$comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); $all_rates=array();

if ( $comments ) { foreach ( $comments as $comment ) { $comment_idi = $comment->comment_ID; $qq_ratei = get_comment_meta( $comment_idi, 'qq_rate', 'true' ); array_push($all_rates,$qq_ratei);
} }

$r = array_filter($all_rates); $rate_average = array_sum($r)/count($r);

// Use a try-catch block to handle errors try { wp_set_comment_status( $comment_id, 'trash' );
update_post_meta( $pid, 'qq_product_rate', $rate_average); $response = array('ostatus' => 'success'); } catch (Exception $e) { $response = array('ostatus' => 'error', 'message' => $e->getMessage()); }

echo json_encode($response); wp_die()

$comment_id = $_POSTcomment_id;$qq_rate = get_comment_meta $comment_id qq_rate true ;$comment_id_data = get_comment $comment_id ; $pid = $comment_id_data-comment_post_ID ;$args = array post_type =

原文地址: https://www.cveoy.top/t/topic/gwal 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录