BP_Group_Member_Query::set_orderby( BP_User_Query $query )

Tell BP_User_Query to order by the order of our query results.

Description

We only override BP_User_Query’s native ordering in case of the ‘last_joined’ and ‘first_joined’ $type parameters.

Parameters

$query

(Required) BP_User_Query object.

Source

File: bp-groups/classes/class-bp-group-member-query.php

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
public function set_orderby( $query ) {
    $gm_ids = $this->get_group_member_ids();
    if ( empty( $gm_ids ) ) {
        $gm_ids = array( 0 );
    }
 
    // For 'last_joined', 'first_joined', and 'group_activity'
    // types, we override the default orderby clause of
    // BP_User_Query. In the case of 'group_activity', we perform
    // a separate query to get the necessary order. In the case of
    // 'last_joined' and 'first_joined', we can trust the order of
    // results from  BP_Group_Member_Query::get_group_members().
    // In all other cases, we fall through and let BP_User_Query
    // do its own (non-group-specific) ordering.
    if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined', 'group_activity', 'group_role' ) ) ) {
 
        // Group Activity DESC.
        if ( 'group_activity' == $query->query_vars['type'] ) {
            $gm_ids = $this->get_gm_ids_ordered_by_activity( $query, $gm_ids );
        }
 
        // The first param in the FIELD() clause is the sort column id.
        $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
        $gm_ids_sql = implode( ',', $gm_ids );
 
        $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
    }
 
    // Prevent this filter from running on future BP_User_Query
    // instances on the same page.
    remove_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
}

Changelog

Changelog
Version Description
BuddyPress 1.8.1 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.