Skip to content

Instantly share code, notes, and snippets.

@danielpowney
Last active August 29, 2015 14:25
Show Gist options
  • Save danielpowney/45d94e480b1791c7957b to your computer and use it in GitHub Desktop.
Save danielpowney/45d94e480b1791c7957b to your computer and use it in GitHub Desktop.
<?php
function mrp_posts_fields( $posts_fields ) {
if ( is_main_query() ) {
$sticky_posts = get_option( 'sticky_posts' );
if ( is_array( $sticky_posts ) && ! empty( $sticky_posts ) ) {
$sticky_posts = implode( ', ', $sticky_posts );
if ( isset( $posts_fields ) ) {
global $wpdb;
$field = "SUM(CASE WHEN ID IN ( $sticky_posts ) THEN 1 ELSE 0 END) AS is_sticky";
if ( ! is_array( $posts_fields ) ) {
$posts_fields .= ', ' . $field;
} else {
array_push( $posts_fields[0], $field );
}
}
}
}
return $posts_fields;
}
add_filter( 'posts_fields', 'mrp_posts_fields' );
function mrp_posts_groupby( $group_by ) {
if ( is_main_query() ) {
$group_by = 'ID';
}
return $group_by;
}
add_filter( 'posts_groupby', 'mrp_posts_groupby' );
function mrp_posts_orderby( $order_by ) {
if ( is_main_query() ) {
$order_by = 'is_sticky DESC, adjusted_star_result DESC, count_entries DESC'; // highest rated
}
return $order_by;
}
add_filter('posts_orderby','mrp_posts_orderby');
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