Show total/aggregate ratings and reviews on the posts/pages

+2 votes
329 views
asked Sep 5, 2016 in Wordpress by Flink
How to show total/aggregate ratings and reviews on the posts/pages?

1 Answer

+1 vote
answered Sep 5, 2016 by Sarath
selected Oct 1, 2016 by mithun
 
Best answer
Here is the code that can work on any post/page

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();

And 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>

For displaying it as,
Overall: 4.67 stars for 60 reviews.

echo "Overall: " . $aggregate_rating . " stars for " . $total_reviews . " reviews.";

Related questions

+2 votes
2 answers 2668 views
+2 votes
1 answer 294 views
0 votes
1 answer 168 views
+2 votes
2 answers 2766 views
+1 vote
1 answer 183 views
asked Oct 14, 2016 in Wordpress by Justin
0 votes
1 answer 308 views
asked Aug 25, 2016 in Wordpress by Sita
...