Title: Category Average
Last modified: November 20, 2017

---

# Category Average

 *  [hash248](https://wordpress.org/support/users/hash248/)
 * (@hash248)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/category-average/)
 * Hi,
    Can you suggest a way to get the sum of all post ratings in a category and
   then get the average ratings of all posts in that category?
 * Thanks in advance!

Viewing 1 replies (of 1 total)

 *  [Cat](https://wordpress.org/support/users/stitchyrocks/)
 * (@stitchyrocks)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/category-average/#post-9874045)
 * I did something like this:
 *     ```
          // Set up an empty array to store average rating for each entry 
           // Run a WP query for entries in this category and find its average rating
           $cat_avg_rating = array();
           $cat_query = new WP_Query( array (
               'posts_per_page' => -1,
               'post_status'    => 'publish',
               'cat' => 1234,
           ));
           if ( $cat_query->have_posts() ) : 
               while ( $cat_query->have_posts() ) : $cat_query->the_post(); 
                   $cat_avg_rating[] = get_post_meta( get_the_ID(), 'ratings_average', true );
               endwhile; wp_reset_postdata();
           endif;
           // Remove empty vals and calculate average
           $cat_avg_rating = array_filter($cat_avg_rating, function($x) { return $x !== ''; });
           $average = array_sum($cat_avg_rating) / count($cat_avg_rating);
           echo $average;
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Category Average’ is closed to new replies.

 * ![](https://ps.w.org/wp-postratings/assets/icon.svg?rev=978014)
 * [WP-PostRatings](https://wordpress.org/plugins/wp-postratings/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-postratings/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-postratings/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-postratings/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-postratings/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-postratings/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Cat](https://wordpress.org/support/users/stitchyrocks/)
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/category-average/#post-9874045)
 * Status: not resolved