Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

How to query and sort posts pages that have assigned reviews by their ranking

Paul Ryley edited this page Oct 19, 2018 · 2 revisions

Site Reviews allows you sort your posts/pages/custom_post_types that have assigned reviews by their overall rating. This is useful, for instance, if you have products with assigned reviews and you wish to show the ones with the highest rating at the top of the page. Site Reviews uses an bayesian algorithm to determine the ranking.

Here is an example of how to make a WordPress query to sort a custom post_type called "product" by rank:

$products = new WP_Query([
    'meta_query' => [
        'relation' => 'OR',
        ['key' => '_glsr_ranking', 'compare' => 'EXISTS'],
        ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'],
    ],
    'order' => 'DESC',
    'orderby' => 'meta_value',
    'post_status' => 'publish',
    'post_type' => 'product',
]);

if( $products->have_posts() ) :
    while( $products->have_posts() ) : $products->the_post();
        glsr_debug( get_the_title() .' - '. get_post_meta( $post->ID, '_glsr_ranking', true ));
    endwhile;
    wp_reset_postdata();
endif;