您的支票不正确:现在,它试图访问$_POST[\'rating\']
, ,然后检查该值是否为空。此外,您应该确定if
语句,因为现在,即使没有提供新的评级,您的代码也会尝试更新平均评级。尝试:
function save_comment_meta_rating( $comment_id ){
if ( ! array_key_exists( \'rating\', $_POST ) || empty( $_POST[\'rating\'] ) ) {
return;
}
$rating = sanitize_text_field( $_POST[\'rating\'] );
add_comment_meta( $comment_id, \'rating\', $rating );
// Then update the average rating.
update_post_avg_rating( $comment_id );
}
add_action( \'comment_post\', \'save_comment_meta_rating\' );