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.
1 2 3 4 5 6 7 8
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->max_rating; $aggregate_rating = $row->aggregate_rating; $total_reviews = $row->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><?php echo ' ' . $total_reviews;?> Reviews</div>
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.
For displaying it as,
Overall: 4.67 stars for 60 reviews.
echo "Overall: " . $aggregate_rating . " stars for " . $total_reviews . " reviews.";