Skip Navigation

[Résolu] How to get rating average from CRED for review

This support ticket is created Il y a 9 années et 11 mois. 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 43 réponses, has 11 voix.

Last updated by joeC-5 Il y a 9 années et 11 mois.

Assigned support staff: Luo Yang.

Auteur
Publications
#111313

triggeru571 - THANK YOU! and you are my HERO!!!
Gosh would buy you a beer (or insert beverage of choice here) if i could!!!

had been completely doing my head in on how to achieve this.

Its 2am atm my time, so will dive into this tomorrow when I have fresh eyes, tyvm for your help though it's deeply appreciated

#111320

Most welcome. It had cost me loads of coffee's and a few sleepless nights (you know when you are in bed and your brain seems to refuse to stop trying to figure out a solution to a problem kind of nights) but thanks to a very stubborn me and Luoy from toolset all works wonderfully 🙂

#111355

No idea why however I get a parse syntax error
Parse error: syntax error, unexpected '[' in /home/whataweb/public_html/typestest/wp-content/themes/dynamik/custom-functions.php on line 40

when i add the following to my custom-functons.php file

//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);
}
#111356

fixed it.. silly error on my part lol 🙂

#112945

Thanks again for this awesome tutorial triggeru571!

Have you done anything to limit the number of reviews per user?
I run a very large directory with another software and see this as a potential issue as my new site grows.

I've detailed what I'm looking to do at the thread linked below but am curious if you've addressed this issue already.
https://toolset.com/forums/topic/limit-author-posts-for-child-cpt/

Thanks again!

Bryan

#133770

Hi triggeru571! Like Bronwyn, I am trying to do the exact same thing.. and hope you are still around. My site consists of a business directory.

I got as far as step 4 and some of 5. But I am getting an error message when I place the code for step 4. The message is:

Parse error: syntax error, unexpected '[' in /pat/path/pathpath/path/functions.php on line 190

Line 190 being "[reviews_total] shortcode:"

Also, I am not using Views yet, but an using a custom template.. is that still going to be okay? I see in step 7 you say to go to Views...

Thanks so much for the tutorial! Hoping I can get it working 🙂

#133791

UPDATE!

With the release of Views 1.3 and Types 1.4 there is a new code you need to use. Please use the code below instead of the above. The rest of the proceedure remains unchanged as per the tutrial: https://toolset.com/forums/topic/how-to-get-rating-average-from-cred-for-review/#post-111308

// 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;
}

Use the following short code produced from above

[rating_average]
//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);
}

Use the following short code to display reviews total

[reviews_total]
#133794

Hi ethanO

I am not sure if the above would work without views as it is designed to work with it. I would really recommend you invest in toolset (note: I am not part of the company - just a very happy client). Toolset will save you countless hours of work, and in my opinion the time savings are well worth the small investment. I figure I have saved close to 300 hours with toolset!

If you still do not want to buy the set, then I suggest you post in their forum how to get this code to work without Views and CRED (you need both for the above to work). Ask for Luoy, he is the brains behind this, and refer him to this topic in your post.

#133836

Hey Trigger!! Yup, I actually have Toolset 🙂 I got it right away after trying the Admin Demo myself.. I have everything installed except for Access..

Hmm, I'm not sure yet I understand how Views works for me... I know how custom templates works as it is doing what it should now. And I am by no means a programmer on any level. Have just figured things out from other posts and trial and error... I have literally tried every WP service out there. Spent money buying plugins, etc.. etc.. and I feel like I can get there with Toolset, and the community seems so much more friendly then any other place.. so really trying to get this working...

This is actually exactly what I am trying to do except also add reviews because my site is a business directory... but I will also have classifieds...

https://toolset.com/learn/wordpress-classifieds-site/

Thanks so much for your help. If you have any more suggestions I'm all ears. 🙂

I may have to start a new thread and hopefully Luoy sees it.. *wink* I've seen him be very active in the majority of threads I scoured through and he seems very helpful..

#133842

Hi ethan), the code above will work with the directories for sure as that is one area I am using it. I do not have a classifieds yet, but I see no reason why it should not work. There is a learning curve with views but it is well worth pursuing. With toolset, you will most likely not need any other plugins for the front end.

#133860

Hey Trigger.. do you have a business directory as well? And when you say the code above will work, are you referring to the code/tutorial that is where the link is?

Going to bed for now. It is almost 5:30 am here!

#133973

Hi ethanO, yes the sites we work on contain directories amongst other things. The code I am reffering to is indeed the one on the tutorial but modified with the new code above because of the new Types/ Views upgrade.

#134211

thanks triggeru571 appreciate you taking the time to update the code.

Most helpful

Cheers
Bronwyn

#134218

Hi Bronwyn, I am having trouble getting this to work.. Would you mind telling me how you went about it?

#134254

HI Trigger, if you are around, could you please email me. I have to have something up by Thursday and I am starting to panic.