i am trying to get and count the values of a meta key (rating) and then calculate the sum. but the following code get all of values in wp_commentmeta
table but i want to get just for the current post. how should i solve that?
$results = $wpdb->get_results("SELECT meta_value FROM {$wpdb->prefix}commentmeta WHERE meta_key = 'rating' ");
foreach($results as $result){
$rate = $result->meta_value;
$sum +=$rate;
}
$res = $sum/count($results);
$res = number_format((float)$res,2,'.','');
and how to count the number of 5 star ratings (the special value) for the current post. the code:
$countstar = $wpdb->get_var("SELECT count(meta_value) FROM {$wpdb->prefix}commentmeta WHERE meta_key = 'rating' AND meta_value = '5'");