Created
July 25, 2015 11:28
-
-
Save danielpowney/716b65779dd402743c59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$query = new WP_Query( array( 'mrp_sort_highest_rated' => true ) ); | |
/** | |
* Adds WP query sort by highest rating then count entries | |
* | |
* @param unknown $order_by | |
* @param unknown $query | |
* @return string | |
*/ | |
function mrp_posts_orderby( $order_by, $query ) { | |
if ( $query->get( 'mrp_sort_highest_rated' ) == true ) { | |
$order_by = 'adjusted_star_result DESC, count_entries DESC'; // highest rated | |
} | |
return $order_by; | |
} | |
add_filter( 'posts_orderby','mrp_posts_orderby', 10, 2 ); | |
/** | |
* WP query join tables for sorting by ratings | |
* | |
* @param unknown $join | |
* @param unknown $query | |
* @return string | |
*/ | |
function mrp_posts_join( $join,$query ) { | |
global $wpdb; | |
if ( $query->get( 'mrp_sort_highest_rated' ) == true ) { | |
$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', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment