Hi Luoy, I am going to resurrect an old topic you and I worked on successfully and which is very popular here on toolset. I am referring to this: https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/
I am using all the code on a different project and it still works fine. What I need to do is sort a view of Parents by the average rating from children. I thought that by implementing this it will work as it should in theory add my average from the reviews to the parent - but all I am getting is a result of 0.
https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/page/3/#post-480639
Below, for convenience sake is all the code taken from that thread and I am indicating what works and what not.
WORKING
// Adds Calculation of average and shortcode for ratings in Views Listings
add_shortcode('rating_average', 'rating_average_func');
function rating_average_func()
{
global $post;
$args = array(
'post_type' => 'review',
'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;
}
//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' => 'review',
'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
'meta_value' => $post->ID,
);
$child_posts = get_posts($args);
return count($child_posts);
}
NOT WORKING
/* Saves average rating and review count*/
//*Update Avg Ratings and Total # of Ratings after review submission
function update_post_review($post_id, $form_data)
{
// if a specific form, change ID to the CRED "Review" ID
if ($form_data['id']==1481)
{
//Get ID of Post Being Reviewed
$parent_post = $_GET['parent_post_id'];
$post = get_post( $parent_post );
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'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;
update_post_meta( $parent_post, 'wpcf-average-rating', $res );
update_post_meta( $parent_post, 'wpcf-total-ratings', $num );
}
}
add_action('cred_save_data', 'update_post_review',10,2);
As you can see from above:
Parent Post: post
Child Post: review
Cred form: 1481
wpcf-ratings is assigned to CPT review (child)
wpcf-average-rating and wpcf-total-ratings assigned to posts but doesnt work when assigned to review either.
What I need to to have wpcf-average-rating and wpcf-total-ratings assigned to post (which I tried) and then have those fields populated so that I can sort posts by highest wpcf-average-rating.
Any tips?
Cheers