如何从评论元中获取值的平均值

时间:2011-05-09 作者:user5233

我使用注释元添加了一个简单的rating 系统用户可以从我添加了3个下拉评论元的评论表单中发布一个速率。

评分很好,反映了评分和用户写的评论。我现在唯一的问题是:我怎样才能得到所有发布的评分的平均值?我需要将平均值放在帖子内容上。

我的评级系统评级如下:

价格、包装、质量我想要每个费率的平均值:

平均价格率、平均包装率和平均质量率

5 个回复
SO网友:MZAweb

如果需要在内容中显示平均值,则需要预先计算它们(在显示注释之前)。

我的方法是在帖子中使用计算出的平均值定制meta,并在每次保存新评论(评级)时修改这些meta。

类似于

add_action("comment_post", "wpse16733_updateAVGs");

function wpse16733_updateAVGs($comment_ID, $approved){

    if ($approved){
        $commentdata=get_comment($comment_ID, ARRAY_A); 
        $parent_post=get_post($commentdata[\'comment_post_ID\']);

        (... get your rating, get post meta, calc and save ...)

    }
}

SO网友:elleeott

我使用自定义查询计算动态平均值得到了类似的结果-根据Rabino的评论,将此函数的结果存储为元值会更有效,但我希望在评论被批准时触发,而不是在保存评论时触发。

以下是您的功能:

function average_rating() {
    global $wpdb;
    $post_id = get_the_ID();
    $ratings = $wpdb->get_results("

        SELECT $wpdb->commentmeta.meta_value
        FROM $wpdb->commentmeta
        INNER JOIN $wpdb->comments on $wpdb->comments.comment_id=$wpdb->commentmeta.comment_id
        WHERE $wpdb->commentmeta.meta_key=\'rating\' 
        AND $wpdb->comments.comment_post_id=$post_id 
        AND $wpdb->comments.comment_approved =1

        ");
    $counter = 0;
    $average_rating = 0;    
    if ($ratings) {
        foreach ($ratings as $rating) {
            $average_rating = $average_rating + $rating->meta_value;
            $counter++;
        } 
        //round the average to the nearast 1/2 point
        return (round(($average_rating/$counter)*2,0)/2);  
    } else {
        //no ratings
        return \'no rating\';
    }
}
在我的情况下,我的评分是1-5分。没有查询结果意味着没有提供评分。

在循环中删除以下内容,就可以开始了:

<?php echo average_rating(); ?>

SO网友:Alex

function set_average_rating( $comment_id ) {
    $comment = get_comment( $comment_id );
    global $wpdb;
    $rating = $wpdb->get_var("       
        SELECT AVG(meta_value) AS avg_rating 
        FROM wp_commentmeta
        WHERE meta_key = \'rating\'
        AND comment_id IN (
            SELECT comment_id
            FROM wp_comments
            WHERE comment_post_ID = $comment->comment_post_ID
            AND comment_approved = 1
        )
    ");
    update_post_meta( $comment->comment_post_ID, \'avg_rating\', round( $rating, 2 ) );  
}
add_action( \'comment_post\', \'set_average_rating\' );
与PaulIsLoud的答案类似,但直接在查询中计算平均值,而不是迭代结果

SO网友:PaulIsLoud

这是我的版本,基于以上两个答案。它继续运行wp_set_comment_status 更改为approve.

calc_avg_rating() 统计字段为的注释rating (如果partuclar注释没有rating, 它只需继续),当新评论获得批准时,它会更新avg_rating.

然后,对于我的模板,我只需调用get_product_rating, 它查看的是avg_rating, 这样,我们就不会在每次加载页面时都计算所有这些内容。

add_action("wp_set_comment_status", "calc_average_rating");

    function calc_average_rating($comment_ID, $approved) {
        if ($approved = \'approve\'){
            $commentdata=get_comment($comment_ID, ARRAY_A); 
            $parent_post=get_post($commentdata[\'comment_post_ID\']);

            global $wpdb;
            $post_id = $parent_post->ID;
            $ratings = $wpdb->get_results("

                SELECT $wpdb->commentmeta.meta_value
                FROM $wpdb->commentmeta
                INNER JOIN $wpdb->comments on $wpdb->comments.comment_id=$wpdb->commentmeta.comment_id
                WHERE $wpdb->commentmeta.meta_key=\'rating\' 
                AND $wpdb->comments.comment_post_id=$post_id 
                AND $wpdb->comments.comment_approved =1

                ");
            $counter = 0;
            $average_rating = 0;    
            if ($ratings) {
                foreach ($ratings as $rating) {
                    $average_rating = $average_rating + $rating->meta_value;
                    $counter++;
                } 
                //round the average to the nearast 1/2 point
                $rating = (round(($average_rating/$counter)*2,0)/2);  
            } else {
                //no ratings
                $rating = \'\';
            }
            update_post_meta($post_id, \'avg_rating\', $rating);
        }
    }

    function get_product_rating() {
        $post_id = get_the_ID();
        $value = get_post_meta($post_id, \'avg_rating\', true);
        return $value;
    }
希望这对别人有帮助!

SO网友:kaiser

此解决方案将在最后一条注释后显示平均值。作为一种解决方法,您只需做两件事have_comments() 注释循环,计算第一个循环的平均评分,并显示第二个循环的注释。

/**
 * You need to place this function inside your comments callback function, so it get\'s
 * triggered with every displayed comment inside the have_comments() loop.
 */
function wpse16733_get_comment_meta_avrg()
{
$divider = (int) $GLOBALS[\'wp_query\']->comment_count;

// initial static values - these get counted up everytime the function get\'s triggered
static $price = 0;
static $packaging = 0;
static $quality = 0;
static $current_comment = 0;

$current_comment = (int) $current_comment++;

// receive all comment meta data
$all_meta = get_comment_meta( get_comment_ID(), \'\' );

// Now get the ratings (it could also be `$avrg_xy = $all_meta->rating` if an object)
$price = (int) $price + (int) $all_meta[\'price\'];
$packaging = (int) $packaging + (int) $all_meta[\'packaging\'];
$quality = (int) $quality + (int) $all_meta[\'quality\'];

// calculate within the last comment
if ( $current_comment == $divider ) 
{
    $average[\'price\'] = $price / $divider;
    $average[\'packaging\'] = $packaging / $divider;
    $average[\'quality\'] = $quality / $divider;
}

// now do stuff with the $average array
foreach ( $average as $rating )
{
    echo \'This is the average rating: \'.$rating;
}
}

结束

相关推荐