WordPress: Show total/aggregate ratings and reviews to your posts/pages

· kalpesh

After struggling for finding the way to show aggregate ratings and total number of reviews for the wordpress plugin WP Customer Reviews, I wrote it myself by studying their code. There has been several requests for this feature in their support page and around but they didn’t seem to be interested or didn’t got enough time to wrote this.

Okay, so for all the people who have been struggling to get total and/or aggregate number of ratings and reviews, here is the code that can work on any page/post.

global $wpdb;  
$pId = $post->ID; //if using in another page, use the ID of the post/page you want to show ratings for.  
$row = $wpdb->get_results(“SELECT COUNT(*) AS `total`,AVG(review_rating) AS `aggregate_rating`,MAX(review_rating) AS `max_rating` FROM wp_wpcreviews WHERE `page_id`= $pId AND `status`=1”);  
$max_rating = $row[0]->max_rating;  
$aggregate_rating = $row[0]->aggregate_rating;  
$total_reviews = $row[0]->total;  
$totl = $aggregate_rating * 20;  
$wpdb->flush();

To show aggregate ratings in star form, simply add this:

<div class="sp_rating" id="wpcr_respond_1"><div class="base"><div style="width: <?php echo $totl;?>%” class=”average”></div>
</div>
<p><?php echo ' ' . $total_reviews;?> Reviews</div>
<p>
Note: In the above code, please check the formatting of PHP tags, sometimes it’s just not formatted proper and as a result there will be wrong ratings displayed.

#aggregate ratings reviews wordpress #wordpress show total ratings #wp customer reviews total aggregate ratings reviews