Star Rating

A visual star rating widget that stores an integer between 1 and the configured maximum. Editors hover to preview and click to set the rating. Ideal for reviews, testimonials, or any scored content.

Settings

SettingDefaultOptionsDescription
Max Stars5110Total number of stars displayed.
Return FormatintintAlways returns an integer.

Return values

Returns a plain PHP integer representing the selected number of stars.

$rating = get_field('review_rating');
// 4  (integer, 1–max_stars)

Returns false or null if no rating has been set.

Usage

Rendering star icons

$rating  = (int) get_field('review_rating');
$max     = 5; // match your field setting

if ($rating) {
    for ($i = 1; $i <= $max; $i++) {
        $class = $i <= $rating ? 'star star--filled' : 'star star--empty';
        echo '<span class="' . esc_attr($class) . '">&#9733;</span>';
    }
}

Displaying with a text label

$rating = (int) get_field('product_rating');
$max    = 5;

if ($rating) {
    echo '<div class="rating">';
    echo '<span class="rating-score">' . esc_html($rating) . ' / ' . $max . '</span>';
    echo '<span class="rating-stars">' . str_repeat('★', $rating) . str_repeat('☆', $max - $rating) . '</span>';
    echo '</div>';
}

Average rating across posts

$args = [
    'post_type'  => 'review',
    'meta_key'   => 'review_rating',
    'meta_type'  => 'NUMERIC',
    'fields'     => 'ids',
    'posts_per_page' => -1,
];

$ids = get_posts($args);
if ($ids) {
    $total = array_sum(array_map(fn($id) => (int) get_field('review_rating', $id), $ids));
    $avg   = round($total / count($ids), 1);
    echo 'Average: ' . esc_html($avg) . ' / 5';
}

WP_Query: filter posts by minimum rating

$posts = get_posts([
    'post_type'  => 'product',
    'meta_query' => [[
        'key'     => 'product_rating',
        'value'   => 3,
        'compare' => '>=',
        'type'    => 'NUMERIC',
    ]],
]);
i
Half-star ratings

Half-star precision (e.g. 3.5) is a Pro feature. The free version is whole-integer only.