0

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?

tim
  • 9,896
  • 20
  • 81
  • 137

1 Answers1

1

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.

shanebp
  • 1,904
  • 3
  • 17
  • 28
  • The "type" parameter doesnt allow me to "keep" / "maintain" my sorting, right? But that's exactly what I was asking for... the ordering comes from my own query beforehand... – tim May 04 '23 at 05:35
  • Answer edited above. – shanebp May 04 '23 at 12:11
  • Hi @shanebp, I found I can hook into `bp_pre_user_query_construct`, also in BuddyBoss, and from there set "user_ids". It works like a charm - BUT one caveat it has: It breaks pagination. Do you have any idea how to use pagination (50 entries per page) and still set the user_ids and ordering? – tim May 05 '23 at 07:03
  • 1
    Confirmed re pagination breaking. I do not have a solution. – shanebp May 05 '23 at 14:32