$49

Star Ratings

Documentation  •  Changelog

Documentation

Get numerical value of stars

Occasionally, you'll need the raw value of the rating, instead of simply drawing a set of stars on the page.

craft.starRatings.avgRating(elementId [, key = null])

This allows you to get the average rating of the specified element (key optional).

{% set avgComfort  = craft.starRatings.avgRating(hotel.id, 'comfortable') %}
{% set avgClean    = craft.starRatings.avgRating(hotel.id, 'clean') %}
{% set avgFriendly = craft.starRatings.avgRating(hotel.id, 'friendlyStaff') %}

{% set avgEverything = (avgComfort + avgClean + avgFriendly) / 3 %}

<p>Grand Average: {{ craft.starRatings.lockedStars(avgEverything) }}</p>

You'll notice that this is also a great case for displaying stars with a fixed value.

Via plugin/module in Craft 3

In Craft 3, you can get the average rating for an element via a plugin or module.

StarRatings::$plugin->starRatings_query->avgRating($elementId, $optionalKey)