how to SELECT for the current post id?

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,’.’,”);

Read more here:: how to SELECT for the current post id?

Leave a Reply

Your email address will not be published. Required fields are marked *