The Extra Theme includes a built-in “star” rating system, which allows users to rate posts out of five stars. Occasionally you may need to manually edit the ratings (e.g. to delete out-dated ratings, or ones you’ve added yourself). Here’s how to do it.
The way the ratings system works is as follows. When a user clicks to leave a rating, the rating value is stored in a “hidden” comment. The average rating for the post is then re-calculated from the rating comments and stored in the post’s metadata. This stored average rating value is then displayed whenever the post is shown.
To allow us to manage the ratings, we can first make the hidden ratings comments visible in the backend, by adding this PHP code to the site:
add_action('init', 'ebc_show_ratings_comments');
function ebc_show_ratings_comments() {
if (is_admin()) {
remove_filter( 'pre_get_comments', 'et_pre_get_comments_filter' );
}
}
As this is PHP code, you can add it to the functions.php of a child theme, or using a plugin such as Code Snippets. Please take a backup prior to adding PHP code to your site, so that you can restore the original version if necessary.
With this code in place, ratings comments will be come visible alongside regular comments in the backend, like so:
You can now moderate these ratings comments as you would other comments: deleting ratings from particular users, changing rating values, etc.
Refreshing the Average Rating on a Post
While the above lets you manage the existing ratings on posts, the average rating shown on a post is only actually updated when a new rating is received. If you are happy to wait, any changes you make to a post’s existing ratings will automatically take effect the next time someone rates the post.
But if you need to update a post’s rating right away, you can use the following PHP code:
add_action('init', 'ebc_recalculate_post_rating');
function ebc_recalculate_post_rating() {
$post_id = 15;
if (function_exists('extra_set_post_rating_average')) {
extra_set_post_rating_average($post_id);
}
}
Set $post_id to the id of the post whose rating you want to refresh. After adding the code, reload the site / post and the average rating should be updated. Note that it would make sense to remove this code once the average rating has been updated, to avoid the overhead of the code being run every time the site is accessed.