计算自定义帖子类型的贝叶斯平均值

时间:2017-03-10 作者:dhuyvetter

我有一个自定义的帖子类型,叫做entries, 它容纳参赛者参加比赛。有一个5星级评级功能,评级存储在entries. 这样我就可以得到ratings_average, ratings_countratings_sum 使用WP_Query:

    $args = array(
        \'post_type\' => \'entry\',
        \'orderby\' => \'ratings_average\',
        \'order\' => \'DESC\',
        \'posts_per_page\' => 10,
        \'post_status\' => \'publish\'
    );
    $loop = new WP_Query($args);
现在,我需要计算贝叶斯平均值或平均值或估计值(据我所知,这只是同一事物的不同名称),这应该可以通过以下SQL查询实现:

SELECT

( SELECT COUNT(*) FROM `bayesian_test` ) AS total_entrants,
( SELECT SUM( vote_count ) FROM `bayesian_test` ) AS total_votes,
( (SELECT total_votes) / (SELECT total_entrants) ) AS average_votes,
( SELECT SUM( rating_count ) FROM `bayesian_test` ) AS total_ratings, 
( (SELECT total_ratings) / (SELECT total_entrants) ) AS average_rating,
post_title,

( ( (SELECT average_votes) * (SELECT average_rating) ) + (vote_count * rating_average) ) / ( (SELECT average_votes) + vote_count) AS mean

FROM 
    bayesian_test
ORDER BY 
    mean ASC;
有没有一种方法可以将SQL语句与WP_Query?

或者,什么是获得相同结果的最佳方法WP_Query 在SQL查询中,我可以在中使用$wpdb?

这个entries 由WordPress存储在wp_posts 具有post_type entry 以及ratings_count, ratings_sum, 和ratings_average 存储在wp_postmeta. 因此,我必须编写一个连接来获取这些结果,然后对结果执行上面的查询。这对DB来说不是很沉重吗?这显示在仪表板小部件中,因此每次有人点击时都会运行查询/wp-admin/.

解决这个问题的最佳方法是什么?

贝叶斯评分/平均值的计算如下:http://fulmicoton.com/posts/bayesian_rating/

1 个回复
最合适的回答,由SO网友:dhuyvetter 整理而成

这是我正在使用的(更新的)代码,它给出了我所期望的结果:

    $args = array(
        \'post_type\' => \'entry\',
        \'orderby\' => \'bayesian_average\',
        \'order\' => \'DESC\',
        \'post_status\' => \'publish\'
    );
    $loop = new WP_Query($args);
    $number_of_entrants = $loop->post_count;
    $total_ratings = $total_num_votes = 0;
    foreach ($loop->posts as $query_post) {
        $count = $query_post->ratings_count;
        $average = $query_post->ratings_average;
        $total_num_votes += $count;
        $total_ratings += $average;
    }
    $average_rating = $total_ratings / $number_of_entrants;
    $avg_num_votes = $total_num_votes / $number_of_entrants;
    if ($loop>have_posts()):
        ?>
        <table class="wp-list-table widefat fixed striped pages">
        <thead>
        <tr>
            <th scope="col" id="entry"><?php _e(\'Entry\', \'textdomain\'); ?></th>
            <th scope="col" id="rating-average"><?php _e(\'Rating average\', \'textdomain\'); ?></th>
            <th scope="col" id="rating-count"><?php _e(\'Rating count\', \'textdomain\'); ?></th>
            <th scope="col" id="beyesian-rating"><?php _e(\'Bayesian Rating\', \'textdomain\'); ?></th>
        </tr>
        </thead>
        <tbody><?php
        global $post;
        while ($loop->have_posts()) :
            $loop->the_post();
            $title = get_the_title();
            $this_num_votes = $post->ratings_count;
            $this_avg_rating = $post->ratings_average;
            $bayesian_average =  (($avg_num_votes * $average_rating) + ($this_num_votes * $this_avg_rating)) / ($avg_num_votes + $this_num_votes);
            update_post_meta(get_the_ID(), \'bayesian_average\', $bayesian_average); ?>
            <tr>
            <td><a href="<?php echo get_permalink(); ?>" target="_blank"
                   title="<?php echo $title; ?>"><?php echo $title; ?>
                </a></td>
            <td>
                <?php echo $post->ratings_average; ?>
            </td>
            <td>
                <?php echo $post->ratings_count; ?>
            </td>
            <td>
                <?php echo round($bayesian_average, 3); ?>
            </td>
            </tr><?php
        endwhile; ?>
        </tbody>
        </table><?php
    else:
        _e(\'No Entries\', \'textdomain\');
    endif;

相关推荐

如何读取WordPress$Query关联数组(散列)键的值

WordPress编程新手(来自更为传统的环境),并试图了解其一些“独特”特性。我们的网站上有一个目录页,此代码驻留在functions.php, 如果条件为true,则调整结果。if( $query->is_post_type_archive( \'directory\' ) ){ ...//do stuff } 我想知道如何获取is_post_type_archive 这就是“目录”当我对值使用测试时。。。var_dumb($query->is_post