Shane is off for the day but I am stepping in to help out until he returns. I will be taking a look at your ticket to assist. Thank you in advance.
Hello George! 🙂
It's very cool of you to chip in this thread and try to help out.
I am appreciate that very much. I should buy you a coffee for reaching out and help user like me in this forum 🙂
I am trying to build review system just like yours in this thread:
https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/page/3/#post-428863
If you don't mind help sharing how you did because I try to follow your steps and I couldn't get average ratings and total review saved.
Yes I have my Restaurants (slug --> restaurants) which is parent of Reviews (slug --> review).
On the reviews CPT I have post fields review title, review comment, and set of radios for ratings (field slug = overall-ratings ).
On the restaurants CPT I have two fields called Ratings Average --> slug = ratings-average and Reviews Total --> slug = reviews-total
I have attached screenshots for detail.
I want to use your codes to calculate averaging overall ratings from radios fields and save it to 'wpcf-overall-ratings field which belong to Restaurants so later can query and display on front-end without calculate it on the fly like other method. I also have 'wpcf-reviews-total' field which also belong to Restaurants.
I would very much thank you for your help to get this to work. I'm not sure what I did wrong that didn't work like your. 🙂
Much Thank,
JSon
Shane
Supporter
Languages:
Inglés (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Json,
Would you mind providing me with admin access to the website so that I can assist better?
Also if this is a test site please let me know.
Thanks,
Shane
This message is for @george .
I forgot to mention on last message to @george that I had try codes you provided but no luck:
//Get ID of Post Being Reviewed
$parent_post = $_GET['parent_restaurants_id'];
$post = get_post( $parent_post );
$args = array(
'posts_per_page' => -1,
'post_type' => 'review',
'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
'meta_value' => $post->ID,
);
Thanks,
JSon
Shane
Supporter
Languages:
Inglés (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Json,
This should work now , I found the problem. It was actually this line
update_post_meta( $parent_post, 'wpcf-ratings-average', $res );
update_post_meta( $parent_post, 'wpcf-reviews-total', $num );
I changed the id being passed to the function to $parent_post since thats what was holding the parent ID.
Please let me know if everything works fine now.
Thanks,
Shane
Hello Shane,
Thank you this form it is now working, however, it stops working again when insert it directly into page. Please see screenshot for better understanding.
Can you please help and why it stops working when insert into the page.
Thanks,
JSon
Shane
Supporter
Languages:
Inglés (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Json,
The reason is because the code needs to be changed so that it gets the id from the current page.
Before it was getting the ID from the url attribute being passed. Change this line from:
$parent_post = $_GET['parent_restaurants_id'];
To
$parent_post = get_the_ID();
Please let me know if this helps.
Thanks,
Shane
Hi Shane,
Thank you for your fast reply 🙂
I tried change to codes above but still doesn't work.
I created new form with id=949 for trouble shoot.
You can see new codes below. Everything the same is except make it works when add form directly on page.
Thank you,
JSon
//*Update Rating For Form 949
function update_post_form_949($post_id, $form_data)
{
// if a specific form, change ID to the CRED "Review" ID
if ($form_data['id']==949)
{
//save overall ratings to database
if (isset($_POST['wpcf-overall-ratings']))
{
// add it to saved post meta
add_post_meta($post_id, 'wpcf-overall-ratings', $_POST['wpcf-overall-ratings']);
}
//Get ID of Post Being Reviewed
$parent_post = get_the_ID();
$post = get_post( $parent_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);
$sum = 0;
$num = 0;
foreach ($child_posts as $child_post) {
$ratings = get_post_meta($child_post->ID, 'wpcf-overall-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-ratings-average', $res );
update_post_meta( $parent_post, 'wpcf-reviews-total', $num );
}
}
add_action('cred_save_data', 'update_post_form_949',10,2);
Shane
Supporter
Languages:
Inglés (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Json,
I added a modification to the code and this should be working now.
Please let me know if this helps.
Thanks,
Shane
Hello Shane,
Thanks for quick reply.
I tried and the review being created but there are two problems.
1. It didn't show up on Post Relationship on Restaurants Post (Parent of Review).
2. Ratings Average and Reviews Total didn't pass on to the parent.
You can try test review at below of post then this time go see the parent post that it suppose to update but didn't.
Please advice.
JSon
Shane
Supporter
Languages:
Inglés (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Json,
The problem is that the parent information wasn't being set.
What I did was to manually create the relationship in the code by adding this line.
update_post_meta($post_id,'_wpcf_belongs_' . $post->post_type . '_id', $parent_post );
The parent information should be added correctly now.
Please let me know if there are other issues 🙂
Thanks,
Shane