databasebuddypressbuddyboss

Custom Members Loop with custom order by


I have a custom Members Loop in Buddypress including pagination (max. 25 entrys per page) where I want to show some members based on a pre-sorted List of user-ids. These user-ids come from a custom table of mine and are sorted based on one column.

The code looks like this:

add_filter( 'bp_after_has_members_parse_args', 'my_custom_users' );
function my_custom_users( $args ) {
    $per_page_setting = 25;
    $page = 1;
    if ( isset( $_REQUEST['vpage'] ) ) {
        $page = absint( $_REQUEST['vpage'] );
    }
    
    global $wpdb;
    $user_ids = $wpdb->get_col('SELECT user_id FROM some_table WHERE some_condition = ' . bp_loggedin_user_id() . ' ORDER BY column DESC');
    
    # @see bp_has_members()
    $args['page']     = $page;
    $args['per_page'] = $per_page_setting;
    $args['max']      = count($user_ids);
    $args['include']  = $user_ids;

    return $args;
}

Everything works as expected, JUST the sorting is off / wrong, because bp_has_members() and bp_core_get_users() and BP_User_Query() do not maintain it even though it could simply only paginate the list of users I have provided using the "include" parameter.

I tried to do it like this, but that doesnt work because it breaks pagination as Buddypress assumes, this is already the FULL list of user-ids:

$args['include']  = array_slice($visitor_ids, ($page-1)*$per_page_setting, $per_page_setting);

What would be the appropriate way of dealing with this?


Solution

  • Have you tried using the type parameter?

    'type' (int) Sort order. Accepts 'active', 'random', 'newest', 'popular', 'online', 'alphabetical'. Default: 'active'.

    So... $args['type'] = 'alphabetical'; afaik, it will be alphabetical based on first letter of first name.

    And do you really need these?

    $args['page']     = $page;
    $args['max']      = count($user_ids);
    

    EDIT: To preserve the order, change $args['include'] to $args['user_ids']. BUT $args['user_ids'] is not available in BuddyBoss, only in BuddyPress.