+2 votes
1.8k views
in Wordpress by
How to show total/aggregate ratings and reviews on the posts/pages?

1 Answer

+1 vote
by
selected by
 
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 9.1k views
0 votes
0 answers 2.8k views
+2 votes
1 answer 1.5k views
+1 vote
1 answer 1.3k views
+2 votes
2 answers 4.9k views
0 votes
1 answer 4.4k views
...