I am trying to: use the advice given by the fantastic @triggeru571 and his thread here: https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/ - 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);
if($ratings)
{
$sum += $ratings;
$num ++;
}
}
$average = 0;
if($num>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
[rating_average]
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:
[reviews_total]
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 -->
<wpv-loop>
<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 />
</div>
</wpv-loop>
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.
</span></div>
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.
Cathie.