+2 votes
2.1k 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.7k views
0 votes
0 answers 3.0k views
+2 votes
1 answer 1.8k views
+1 vote
1 answer 1.6k views
+2 votes
2 answers 5.5k views
0 votes
1 answer 4.7k views
...