Reply To: Order by custom field in member loop
I tryed to work also with this solution (provided by @shanebp too):
https://buddypress.org/support/topic/creating-a-new-order-by-filter-in-groups-loop-with-custom-sql/
but I do not obtain to order members by rating.. is this the right approach??
function antares_members_filter_options() {
echo '<option value="rating">rating</option>';
}
add_action( 'bp_members_directory_order_options', 'antares_members_filter_options' );
function antares_ajax_querystring( $query_string, $object ) {
if ( 'members' != $object )
return $query_string;
if ( ! bp_is_members_directory() )
return $query_string;
$query_args = wp_parse_args( $query_string, array() );
$page = $query_args['page'];
if( isset( $query_args['action'] ) && $query_args['action'] == 'rating' ) {
$query_args = array();
$query_args['page'] = $page;
$query_args['orderby'] = 'rating';
$query_args['order'] = 'ASC';
$query_args['include'] = antares_get_members();
$query_string = http_build_query( $query_args );
}
return $query_string;
}
add_filter( 'bp_ajax_querystring', 'antares_ajax_querystring', 32, 2 );
function antares_get_members() {
global $wpdb;
$sql = "SELECT
a.ID as id, a.user_login AS name, a.display_name AS displayname, AVG(star) AS rating, COUNT(star) AS reviews
FROM
{$wpdb->prefix}users AS a
LEFT JOIN
{$wpdb->prefix}bp_activity AS b ON a.ID = b.usercheck
WHERE
(b.is_activated is null or b.is_activated=1)
GROUP BY
id
ORDER BY
rating DESC";
$buff = array();
$result = $wpdb->get_results( $sql , OBJECT );
foreach ($result as $row) {
$buff[]= $row->id ;
}
$query_str= implode (',', $buff);
return $query_str;
}