Skip Navigation

[Resuelto] Sort by highest rated

This support ticket is created hace 5 años, 10 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 – 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)

Autor
Mensajes
#481258

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

#481270

Luo Yang
Supporter

Languages: Inglés (English ) Chino simplificado (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Dear triggeru571,

Since it is a custom PHP codes problem, please provide a test website with same problem, fill below private detail box with login details and ftp access, also point out the problem page URL and CRED form URL, and where I can see the sort result is working or not working. I need a live website to test and debug. thanks

#481308

Luo Yang
Supporter

Languages: Inglés (English ) Chino simplificado (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the details, I am checking it in your website, will feedback if there is anything found

#481327

Luo Yang
Supporter

Languages: Inglés (English ) Chino simplificado (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I tried the SFTP access you provided, seems it does not work, please check it.
I get this error:
open "financingtimes-sftpstaging@35.185.40.126" 22
Error: Connection timed out after 20 seconds of inactivity
Error: Could not connect to server

#481465

Hi Luoy. I managed! I had a silly error and inverted the CPT's 🙂 Thanks a million for looking into this anyway.