Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; // Don't update ratings if review system is disabled } $getAvgRatings = 0; // Only get parent comments (reviews), not replies $comments = get_comments( [ 'post_id' => $id, 'parent' => 0, // Only parent comments (no replies) 'status' => 'approve', ] ); if ( $comments ) { $i = 0; $total = 0; foreach ( $comments as $comment ) { $rate = get_comment_meta( $comment->comment_ID, 'adqs_review_rating', true ); if ( isset( $rate ) && '' !== $rate ) { ++$i; $total += $rate; } } if ( $i > 0 ) { $getAvgRatings = round( $total / $i, 1 ); } } $meta_key = sanitize_key( 'adqs_avg_ratings' ); update_post_meta( $id, $meta_key, $getAvgRatings, get_post_meta( $id, $meta_key, true ) ); if ( ! adqs_get_parent_reviews_count( $id ) ) { delete_post_meta( $id, $meta_key ); } } }