i have a rating system in comments, for each comment user can rate 1-5 Stars.

Now , i want the average of all ratings in comments of that post to show.

How can i do that.

Here is the link to the code file related to My rating System. Current Users can rate and comment. But i want the average of all comment ratings in that post. And this is a custom Post type.

check this link for the code related to rating used in the site.


  • What should i write in <li> </li> to create tab called My Reviews and in that tab show users comments with Post title – luckyrajiv Sep 6 '13 at 16:21
  • what's the output of average ratings you are getting now? – Anjum Sep 6 '13 at 19:44
  • i dont have an average function added to single-places.php, currently only people/users can comment with their rating. and the above pastebin page is the code for all the rating function. now i want to create a function to call to give me the average of all comment rations in a post to show. – luckyrajiv Sep 6 '13 at 19:47
  • first of all wrap your table creation/checking code in a function and hook into plugin activation hook, this will save some resources. – Anjum Sep 6 '13 at 19:56
  • how can i do that ? – luckyrajiv Sep 6 '13 at 19:56

add this code into your plugin file to register activation hook

// register activation hook
register_activation_hook( __FILE__, 'my_rating_plugin_activation' );

now create a function and add your table creation code into

function my_rating_plugin_activation() {
    global $wpdb, $rating_table_name;
    if($wpdb->get_var("SHOW TABLES LIKE \"$rating_table_name\"") != $rating_table_name) {
        $wpdb->query("CREATE TABLE IF NOT EXISTS $rating_table_name (
            rating_id int(11) NOT NULL AUTO_INCREMENT,
            rating_postid int(11) NOT NULL,
            rating_posttitle text NOT NULL,
            rating_rating int(2) NOT NULL,
            rating_timestamp varchar(15) NOT NULL,
            rating_ip varchar(40) NOT NULL,
            rating_host varchar(200) NOT NULL,
            rating_username varchar(50) NOT NULL,
            rating_userid int(10) NOT NULL DEFAULT '0',
            comment_id int(11) NOT NULL,
            PRIMARY KEY (rating_id)
        ) ENGINE=MyISAM");
| improve this answer | |
  • shall i add this function directly into the code i have provided. or do i need to delete some code from the existing template and add this one somewher – luckyrajiv Sep 6 '13 at 20:12
  • i added the above code at the end of the template before ?> – luckyrajiv Sep 6 '13 at 20:14
  • add activation hook and function by replacing table creation code from plugin and place it at same place where the table creation code is. – Anjum Sep 6 '13 at 20:24
  • i did that, but how will i get to show the average of all comment ratings – luckyrajiv Sep 6 '13 at 20:29
  • Can you see the code around Line no 69 which says get average ratings. can you modify that and call the function to display average ratings – luckyrajiv Sep 6 '13 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.