Skip to content

Instantly share code, notes, and snippets.

@danielpowney
Last active August 29, 2015 14:14
Show Gist options
  • Save danielpowney/3c9dbcb669de98c795d5 to your computer and use it in GitHub Desktop.
Save danielpowney/3c9dbcb669de98c795d5 to your computer and use it in GitHub Desktop.
<?php
/**
* Adds WP loop sort by highest rating then count entries
*
* @param unknown $order_by
* @param unknown $query
* @return string
*/
function mrp_posts_orderby( $order_by ) {
if ( is_main_query() ) {
$order_by = 'adjusted_star_result DESC, count_entries DESC'; // highest rated
}
return $order_by;
}
add_filter('posts_orderby','mrp_posts_orderby');
/**
* WP loop join tables for sorting by ratings
*
* @param unknown $join
* @param unknown $query
* @return string
*/
function mrp_posts_join( $join ) {
if ( is_main_query() ) {
global $wp_query, $wpdb;
$general_settings = (array) get_option( MRP_Multi_Rating::GENERAL_SETTINGS );
$rating_form_id = $general_settings[MRP_Multi_Rating::DEFAULT_RATING_FORM_OPTION]; // you could remove this from the join if you want
$filters_hash = MRP_Multi_Rating_API::get_filters_hash( array() ); // if you want to add any Multi Rating pro specific filters e.g. rating_item_ids
$join .= ' LEFT JOIN ' . $wpdb->prefix . MRP_Multi_Rating::RATING_RESULT_TBL_NAME . ' rr ON ' . $wpdb->posts . '.ID'
. ' = rr.post_id AND rr.rating_form_id = ' . $rating_form_id . ' AND rr.filters_hash = "' . $filters_hash
. '" AND rr.rating_item_id IS NULL AND rr.rating_entry_id IS NULL';
}
return $join;
}
add_filter('posts_join', 'mrp_posts_join');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment