Skip Navigation

[Cerrado] Setting up a ratings average for reviews CPT

This support ticket is created hace 7 años, 9 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 17:00 9:00 – 17:00 9:00 – 17:00 9:00 – 17:00 9:00 – 17:00 -
- - - - - - -

Supporter timezone: Europe/Athens (GMT+03:00)

This topic contains 5 respuestas, has 2 mensajes.

Last updated by Konstantinos hace 7 años, 9 meses.

Assigned support staff: Konstantinos.


I am trying to: use the advice given by the fantastic @triggeru571 and his thread here: - the below guidance is what I've copied and followed from the original thread, the only thing changed was the upgraded code from on page 2 of the original thread.

1) Create a new Custom Field, name it 'rating' and choose Radio Buttons. You need to give each button the following values 0,1,2,3,4,5 and select 0 as default. Create any other fields you may need for your reviews. In my case I had review title and description.
2) Create a Custom Post Type and name it 'reviews'
3) Create a CRED form to display the fields you just created in your Custom Field.
4) Add the code below to your functions.php file. I placed mine in a custom functions file to keep my file clean, but if you do not have one, funtions.php will be fine

// Adds Calculation of average and shortcode for ratings in Views Tips
add_shortcode('rating-average', 'rating_average_func');
function rating_average_func()
    global $post;
    $args = array(
       'post_type' => 'reviews',
        'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
        'meta_value' => $post->ID,
    $child_posts = get_posts($args);
    $sum = 0;
    $num = 0;
    foreach ($child_posts as $child_post) {
        $ratings = get_post_meta($child_post->ID, 'wpcf-ratings', true);
            $sum += $ratings;
            $num ++;
    $average = 0;
        $average = $sum/$num;
    $res = $average;
    if($average==0) $res = 0;
    if($average>0.001 && $average<0.5)$res = 0.5;
    if($average>0.501 && $average<1) $res = 1;
    if($average>1.001 && $average<1.5) $res = 1.5;
    if($average>1.501 && $average<2) $res = 2;
    if($average>2.001 && $average<2.5) $res = 2.5;
    if($average>2.501 && $average<3) $res = 3;
    if($average>3.001 && $average<3.5) $res = 3.5;
    if($average>3.501 && $average<4) $res = 4;
    if($average>4.001 && $average<4.5) $res = 4.5;
    if($average>4.501 && $average<5) $res = 5;
//... here put more condition ...
    return $res;

What we have done above is create the short code to display the average rating in increments of 0.5. Your short code will now be


5) Add this code beneath the code above

//Calculates and displays the number of reviews in a post
add_shortcode('reviews_total', 'reviews_total_func');
function reviews_total__func()
    global $post;
    $args = array(
'posts_per_page'   => -1,
       'post_type' => 'reviews',
        'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
        'meta_value' => $post->ID,
    $child_posts = get_posts($args);
    return count($child_posts);

With the code above, we are counting the number of reviews we have in a post. This is an important function for Schema. The short code created by this is:


6) Next in line, you need to create your stars (or any imagery you want). In total you are going to have to create 11 images for the following options: 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5 . Give each image a name + the value assigned to it. For example, mine are called stars0.png, stars0.5.png, stars1.png etc. Upload them to your folder. Mine is uploaded to a folder 'ratings which I created in my main images folder: images/ratings.

7) Go to the View which will generate the necessary loop to display your reviews and add the following code:

<!-- wpv-loop-start -->
     <div><strong>[wpv-post-title]</strong> | <img src="<em><u>hidden link</u></em> field="ratings"][/types].png" alt="Rated [types field="ratings"][/types] out of 5"/></div>
     <div><strong>Review by:</strong> [wpv-post-author] on [wpv-post-date]</div>
     <div>[wpv-post-body view_template="None"]
       <hr />

Note what has been done here. We have called [types field="ratings"][/types] and placed it to replace the value of the star within our image, so that star1.png becomes star[types field="ratings"][/types].png
So now when your view is loaded on the front end, the types shortcode will be replaced by the value selected on the review and will display your star images.

8) Go to your Content Template which your reviewd item will be using, and put this code in it. Place it where you want your star rating to appear.

<div class="rating-box"> 
<span itemprop="aggregateRating" itemscope itemtype="<em><u>hidden link</u></em>">
<meta itemprop="ratingValue" content="[Rated [rating-average] out of 5]">       
    <img src="<em><u>hidden link</u></em>" alt="Rated [rating-average] out of 5"/> from <span itemprop="ratingCount">[reviews_total]</span> reviews.

On my view template, I also called the View which displays the actual reviews and the individual star ratings given by each reviewer (step 7).

9) Make sure all the paths and file names are correct for your own site settings. Give one of your items a review with stars and check it out on the front end. It should now display your stars.

A word on Schema:

You need to customize the Schema depending on what your site is. You already have a sample of how it would look like in (8) above and you can leave the values as they are. However, for the snippets to show up in the search engine, you need to add much more into your Content Template (ex: type of Schema you will use, product name etc.) . Many of the tutorials out there are good though for some reason many say Schema is complex. IT IS NOT and its as easy as writing basic HTML.

. . .

I visited this URL: hidden link

I expected to see: that the average and total rating would work correctly but it doesn't. The star's are empty and the reviews total doesn't display a number .. just the [reviews_total] shortcode brackets.

Instead, I got: the individual rating to work and show, but NOT the average rating or the review total. You can see this at the top of the content.

I don't know what has changed in the Views upgrades since 2013 that would make the average rating and other code stop working.

Can you help please?

Thank you.


I found that the reviews_total doesn't work with the updated code .. the original code from page 1 of the original thread works instead .. ie:

//Calculates and displays the number of reviews in a post [reviews_total] shortcode:
add_shortcode('reviews_total', 'reviews_total_func');
function reviews_total_func()
$child_posts = types_child_posts('reviews');
return count($child_posts);

^^ this does work.


Dear Cathie,

Thank you for contacting Toolset support.
I am glad to hear that you managed to resolve your issue.

Please let me know if I can help you further.



Hi there, I managed to get the [review_total] to work.. sort of but not the [ratings-average]. That's the section I need help with now.

I've added a new line which shows just the number of the ratings average near the top. I have added two rating reviews, a 1 star and a 5 star review. I reckon that should show a 3 star review .. yet the ratings average output is '0'.

I don't understand the code enough provided by the other thread to understand what is going wrong. I don't know if this is due to upgrades to the toolset changing things or something else.

The code references 'wpcf-ratings' and I need to change this .. but everything I try fails. I have a custom post type of vendor-review and a custom field within that called review-rating. When I try 'wpcf-review-rating' it still doesn't count correctly.

How do I enter the correct 'field name' into this part of the code:

$ratings = get_post_meta($child_post->ID, 'wpcf-ratings', true);

Can you still help me please?



Thank you for the information.

I would like to request temporary access (wp-admin and FTP) to your site, in order to take a better look at the issue. Also, I would like to ensure that this is a development site, as we can't test if it's a live site.

You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Please remember to backup your database first before you proceed! You can use a plugin for this, if you like (example:



Thank you for the information.

I managed to resolve the issue. In your "rating_average_func()" you should change:

$args = array(
	'post_type' => 'reviews',
	'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
	'meta_value' => $post->ID,
$child_posts = get_posts($args);


$child_posts = types_child_posts('vendor-review');

in order to get the child ratings for each vendor.

Now it seems to work as expected.


El debate ‘[Cerrado] Setting up a ratings average for reviews CPT’ está cerrado y no admite más respuestas.