I have a custom post type called entries
, which holds contestants for a competition. There is a 5 star rating function, the ratings are stored with the entries
. So I can get the ratings_average
, ratings_count
and ratings_sum
with a WP_Query
:
$args = array(
'post_type' => 'entry',
'orderby' => 'ratings_average',
'order' => 'DESC',
'posts_per_page' => 10,
'post_status' => 'publish'
);
$loop = new WP_Query($args);
Now I need to calculate the Bayesian average or mean or estimate (which are just different names for the same thing as far as I can tell), which should be possible with this SQL query:
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;
Is there a way to combine SQL statements with WP_Query
?
Or what is the best way to get the same results I get from WP_Query
in a SQL query I can work with in $wpdb
?
The entries
are stored by WordPress in the wp_posts
with post_type
entry
and the ratings_count
, ratings_sum
, and ratings_average
are stored in wp_postmeta
. So I’d have to write a join to get those and then do the above query on the result. Wouldn’t this be heavy on the DB? This is shown in a Dashboard widget, so the query would run every time someone hits /wp-admin/
.
What is the best (as in most efficient) way to tackle this?
The Bayesian rating / average is calculated like this: http://fulmicoton.com/posts/bayesian_rating/