Advertisement
geminilabs

[site-reviews] sort pages by ranking

Feb 23rd, 2019
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. // Page that have reviews assigned to them are given a special number that determines their ranking.
  2. // The page ranking is calculated from the number of assigned reviews and their ratings.
  3.  
  4. $query = new WP_Query([
  5.     'meta_query' => [
  6.         'relation' => 'OR',
  7.         ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], // this comes first!
  8.         ['key' => '_glsr_ranking', 'compare' => 'EXISTS'],
  9.     ],
  10.     'order' => 'DESC',
  11.     'orderby' => 'meta_value_num',
  12.     'post_status' => 'publish',
  13.     'post_type' => 'page', // change this as needed
  14.     'posts_per_page' => 10, // change this as needed
  15. ]);
  16.  
  17. if( $query->have_posts() ) {
  18.     while( $query->have_posts() ) {
  19.         $query->the_post();
  20.         $average = sprintf( '%s (average rating: %s)',
  21.             get_the_title(),
  22.             get_post_meta( $post->ID, '_glsr_average', true )
  23.         );
  24.         $ranking = sprintf( '%s (ranking: %s)',
  25.             get_the_title(),
  26.             get_post_meta( $post->ID, '_glsr_ranking', true )
  27.         );
  28.         apply_filters( 'glsr_debug', 'Site Reviews is not installed', $average, $ranking );
  29.     }
  30.     wp_reset_postdata();
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement