WordPress: Show total/aggregate ratings and reviews to your posts/pages
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[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:
1 |
<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.
1 |
echo "Overall: " . $aggregate_rating . " stars for " . $total_reviews . " reviews.";
|
31 Comments
Leave a comment
Welcome to my Blog
Categories
- checkout (4)
- eCommerce (2)
- humor (3)
- JSON (1)
- Linux (4)
- Magento (73)
- Magento admin (32)
- Magento Certification (2)
- Magento error (6)
- Magento frontend (32)
- Magento Interview (5)
- mobile (1)
- MySQL (5)
- PHP (6)
- Ubuntu (3)
- Wordpress (1)
- XML (2)

This blog is merely to record the things that I find interesting. It can be technical, humor, news, creative or anything that deserves to be posted here. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!
hi, will this work in category.php? i tried inserting the code in category.php, but nothing shows up. thanks
have you echo’ed the variables you need to show? Replace post/page id you want to show ratings and reviews for, it should work I guess, although I have not tried in category.php.
I am not very fluent with php and I am having a heck of a time adding your code to my existing site.
The first part of your code I have tried adding to the file “wp-customer-reviews.php” and I have also tried adding it to both my single-page and single-post template files in my wordpress theme.
No matter where I have tried to place the code I get the same results. Also if I don’t place the first block of code at all, I also get the same results. The results are as follows:
Anywhere I place the second piece of code you gave under “To show aggregate ratings in star form, simply add this:” returns the same result of always displaying 5 stars no matter what the ratings are and even if there are no ratings.
Anywhere I place the third piece of code you gave under “For displaying it as,” Give me the results of “Overall: stars for reviews.” but never displays any numbers.
Okay I finally figured out where to put the code, but it only works if I manually add the post/page id. Its also fairly useless to me until I figure this out because I can’t exactly manually add posts/pages when the posts/pages are using template files.
Any suggestions for me?
The real problem seems to be that I am using the plugin on pages only, not posts and i have dozens of pages. In fact to even get the plugin to work with my install I had to manually add to my template files because the plugin wouldn’t recognize my pages.
Any suggestions for me?
You don’t need to add page id manually if you want to show ratings on the same page. There will be already postID in the object which you can assign, it should be dynamic.
In what file must the code be placed?
anywhere, just don’t forget to assign post/page id (for which you want to show ratings) to $pId
thanks for your quick response.
What do you mean by ‘anywhere’?
in content.php? menu.php? Something else?
By anywhere, i mean anywhere. it must be PHP file and the WP customer review plugin must be enabled ofcourse.
We are just querying the DB table, which is available for all php files (header, menu, content, footer, sidebar, etc.), just you need to pass the proper post/page id, that’s it!
Thanks for this code.
However I got a little problem I can’t seem to solve.
The variables are empty ($total_reviews, $aggrerate_rating & $max_rating)
When I run the code in phpmyadmin everything works fine.
I’m trying to get this to work in single.php
Just make sure $pId has value of your post/page id. I guess it doesn’t. You can even echo the whole query and run it in phpMyAdmin. If it has the id, print_r the $row to check if anything is retrieved, if not then you need to pass the post/page id in order to work.
I’m having trouble adding this code. I copied and pasted the code into page.php, but nothing showed up. I tried changing the $pId line to read
$pId = $post->9;
because the page ID # is 9. Is this correct? Should I also add before and after the code?
Veronica, it’s incorrect. The $pId should be 9 and not $post->9;
In short, replace $pId = $post->9;, with $pId = 9;
Let me know if you still face the problem.
Thank you! I put this into one of the php files for the theme I’m using, but now the code is showing up on the screen, and not only on pId9 but on all pages. Any idea why?
You need to give some condition that will identify on which page you want to show it.
Hey Kalpesh,
First of all thanks for all the great work that you have done for fixing this issue.
Could you please give an example in code for a condition that Veronica could set? For example,
$pId = 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sorry I have been banging my head against this for a while now. I am really just trying to get the “count” to work and am having a ton of trouble.
Thanks in advance, any help is appreciated!!!!!
Thanks Ryan.
Although I don’t know where and how you want this code to show effect, this post will definitely help you in understanding conditions to set http://codex.wordpress.org/Conditional_Tags
Thanks for your work making this but
I’m lost, I installed main code in wp-customer-reviews.php
I insert
it shows 5 empty stars and the text reviews (it has one review on page with 4 stars)
the second code (pasted in html mode) shows exactly this;
echo “Overall: ” . $aggregate_rating . ” stars for ” . $total_reviews . ” reviews.”;
I thought if I used code on same page as revews I don’t have to change anything?
Is there an error in the second code?
You don’t need to insert this code in wp-customer-reviews.php, you have to put it in single.php or wherever you want to show this output.
In the second code, just check if you have correct double quotes. As said, you don’t have to put the code in reviews page, you have to put it in post page with correct post/page id.
HTH
Dont know what all the fuss is about, I just put the first part of the code right at the top of my brand-category.php
and then put the 2nd part above my current WPCR echo’d shortcode, and it works perfectly! THANK YOU VERY MUCH!
I think some people might be getting it wrong by not enclosing the code php tags…
Hi,
i have 2 questions:
1.)
i have around 100 business names , each on a different “post” and the plugin is installed.
i want to pull the overall rating of each business name into a table which is on a separate “page”. Each overall rating will be in a separate cell of the table.
How will i do that?
Do i have to put all the post IDs ?
i am lost within the codes
2.) And i want to display the overall rating of a business name at the top of the same post as stars
::::::::::::::::::::::::::::::
And i am thinking it would be easier if there is a way to do these with shortcodes.
You need to provide the post’s ID in order to get overall/average rating for that post. You need not worry about total business names you have for each post, it will automatically be taken care once you provide the post’s ID. Please read the post for how to do it.
Hi,
I have used your code to display star ratings on a page that displays a list of properties and it works, however I wanted to use the stars and I used the code you provided however it doesn’t matter what the rattings are I see 5 orange stars (even if the ratings are 2 stars etc.) I hope you can help.
Sorry worked it out, was a copy and paste error. All fixed now.
Great!
Thanks, its working for me, I was getting struggle since one week. Great dude
One of the problem with this code can be Table Prefix: in Kalpesh’s code it’s wp_ (wp_wpcreviews). To check your table prefix open wp-config.php and find the code:
$table_prefix
For example my table prefixe is $table_prefix = ‘wrd_’; and i changed wp_wpcreviews to wrd_wpcreviews
Hope it will help someone.
Nice tutorial thanks very much.
Just one issue, i wanted to use this so that the stars/rating would show up on Google search pages under my site. Using the rich snippet testing tool it doesn’t show for any pages other than the review pages itself (which it did already).
Any way to correct this?
Thanks in advance.
Hi, kalpesh
thank you so much for your snippet which works like a charm !
Is it possible to display only one decimal instead of 4 ? like 3,0 instead of 3,0000
thank you so much !
Nice contribution Kalpesh,
I’m having an issue with the output I’m hoping you can help. I have 3 test reviews posted and it just echos “Overall: stars for 0 reviews”. I’m using it for a multisite install so I’m wondering if there might be an issue there. I changed wp_wpcreviews to the specific network site wp_4_wpcreviews with no luck, I get the same result when using the star version as well.
Don’t know why you are getting empty results. I would suggest you to run this query in phpMyAdmin or mysql console directly to see if it fetches any records or not.
Make sure you replace $pId with your post/page ID, wp_wpcreviews with your table name.