Skip to content

Instantly share code, notes, and snippets.

@mgmartel
Last active December 11, 2023 03:23
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mgmartel/4463855 to your computer and use it in GitHub Desktop.
Save mgmartel/4463855 to your computer and use it in GitHub Desktop.
Sort BuddyPress users by last name in the members directory. Updated for BP 1.7.
<?php
// Sort BuddyPress users by last name
//
// Use this Gist to sort members alphabetically by last name in the
// members directory. Follow the instructions below and drop the code
// in your functions.php or your functions plugin.
//
// Updated to work with BP 1.7. See earlier revisions of this gist for
// earlier versions of BP.
/**
* Sort users by last name
*
* Changes the querystring for the member directory to sort users by their last name
*
* @param BP_User_Query $bp_user_query
*/
public function alphabetize_by_last_name( $bp_user_query ) {
if ( 'alphabetical' == $bp_user_query->query_vars['type'] )
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)";
}
add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );
// To reflect the alphabetical sorting of the member directory, you
// could also format the names to show the last name first. Here is
// a helper function to format names as follows: Last, First Middle
// Etc.
//
// To use this function every time a user's name is shown or queried,
// simply add it as a filter:
//
// `add_filter ('bp_get_member_name', 'format_last_name_first' );`
//
// To only reformat the name in the member directory, change your
// members/members-loop.php file in your (child)theme. Look for
// `bp_member_name()`
// and replace it with
// `echo format_last_name_first ( bp_get_member_name() );`
/**
* Helper function for formatting a name: Last, First Middle Etc.
*
* @param string $name
* @return string Formatted name
*/
function format_last_name_first( $name ) {
if ( $first_space = strrpos( $name, " ") )
$name = substr( $name, $first_space + 1 ) . ", " . substr( $name, 0, $first_space );
return $name;
}
// BONUS: To make alphabetical sorting the default sorting, use the
// function below.
//
// In order for this to work properly, `<option value="alphabetical">`
// must be the first option for the members-order-select in
// members/index.php.
function sort_members_alphabetically( $qs = '', $object = false ) {
if( $object != 'members' ) //for members only
return $qs;
if ( empty( $qs ) || ! strpos( $qs, "type=" ) ) {
$args = wp_parse_args($qs);
$args['type'] = 'alphabetical';
$args['action'] = 'alphabetical';
$qs = build_query( $args );
}
return $qs;
}
add_action( 'bp_ajax_querystring', 'sort_members_alphabetically', 11, 2 );
@MrUzu
Copy link

MrUzu commented May 27, 2013

Thanx again for the update ;)

@MrUzu
Copy link

MrUzu commented May 27, 2013

I can't get it to work with the 1.7.2 right now...
anyway, i'll get back to you if i get to understand what happens.

@MrUzu
Copy link

MrUzu commented May 27, 2013

Hum... well after a few minutes trying to debug the 'alphabetize_by_last_name' function, i found that this works better to me :

function alphabetize_by_last_name( $bp_user_query ) {
if ( 'alphabetical' == $bp_user_query->query_vars['type'] )
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.value, ' ', -1)";
}
add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );

@susanlangenes
Copy link

Thank you for this! I did the same as above; just removed 'public'

@cairam
Copy link

cairam commented Mar 31, 2014

Hi, anyone can help me?
I used this code on my custom function but it didnt work.

@hnazar
Copy link

hnazar commented Dec 3, 2014

Hi, I am also stuck. I put this code in the functions.php file of my child theme and no luck.

Any help would be appreciated!

@afi13
Copy link

afi13 commented Jul 22, 2015

How sort members by custom profile field?

@gsusI
Copy link

gsusI commented Nov 30, 2020

function sort_members_alphabetically( $qs = '', $object = false ) {
    if( $object != 'members' ) //for members only
        return $qs;

    if ( empty( $qs ) || ! strpos( $qs, "type=" ) ) {
        $args = wp_parse_args($qs);
        $args['type'] = 'alphabetical';
        $args['action'] = 'alphabetical';

        $qs = build_query( $args );
    }

    return $qs;
}
add_action( 'bp_ajax_querystring', 'sort_members_alphabetically', 11, 2 );

Replace

 if ( empty( $qs ) || ! strpos( $qs, "type=" ) ) {

with:

 if ( empty( $qs ) || (strpos( $qs, "type=" )  === false) {

@CordialGit
Copy link

@[mgmartel]

Today 11th of December 2023, I am using BuddyPress 11.4.0

And Yes, I have struggled on how to Sort BuddyPress Members in Alphabetical Order. I think I've still not found the solution.

Can You please extend this Code to be 2 in 1?

By this, I mean that it should enable us to sort by:
a.) Alphabetical Order
b.) First Name and Last Name.

Can You Please extend this code to be able to do this?

And can you see to it, that the code will be compatible with BuddyPress 11.4.0 ?

Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment