You don’t show how you call function format_last_name_first.
But this approach is probably better:
// members loop - sort by last name if type== alphabetical
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' );
As shown, it will do what your function is doing. You will have to change the ‘-1’ variable, probably to ‘1’. More info.
Hi Shane,
Thanks for yourvreply but I already used that snippet. If I try to edit the -1 variable to 1 the members get sorted by their first name.
Setting it to 0 does nothing and -2 and beyond all sorts the members by their first name.
substring_index()
doesn’t quite seem to do the job.
How about something like:
substr( strstr( 'Robin van Persie', ' ' ), 1 )
Which would give you van Persie
Nope, it returns an error that there are no members to display. I guess that I could replace ‘Robin van Persie’ with something blank like this
substr( strstr( ' ', ' ' ), 1 )
Just to clarify, this is the full code in my functions.php to sort the members:
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:
//
// <code>add_filter ('bp_get_member_name', 'format_last_name_first' );</code>
//
// To only reformat the name in the member directory, change your
// members/members-loop.php file in your (child)theme. Look for
// <code>bp_member_name()</code>
// and replace it with
// <code>echo format_last_name_first ( bp_get_member_name() );</code>
/**
* 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, <code><option value="alphabetical"></code>
// 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 );
I guess that members are sorted by the default WordPress first and last name fields right? Just because we’ve added some custom fields to the member profile pages to split their names in first name, ‘surname prefix (it’s for words like ‘van’ ‘de’) and last name. Not all members have a surname prefix though.
Did you replace this line:
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)";
with this:
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substr( strstr( u.display_name, ' ' ), 1 )";
?
Yes, only left u.display_name
out of it but either way it retuns a ‘Members not found’ message in return.
Try this:
"ORDER BY substring_index( substring_index(u.display_name, ' ', 2), ' ', -1 )";
Jackpot! That did the trick, awesome!
Thanks so much.
@shanebp the only problem with substring_index( substring_index( 'Robin van Persie', ' ', 2), ' ', -1 )
is you lose anything after the 2nd delimiter. For example:
substring_index( 'Robin van Persie', ' ', 2)
would return Robin van
and then
substring_index( 'Robin van', ' ', -1)
would return van
So in theory, the order returned could be
Denis Bergkamp
Stefan de Vrij
Barry van Galen
Robin van Persie
Ruud van Nistelrooy
Wesley Sneijder
As you can see Robin van Persie and Ruud van Nistelrooy are in the wrong order.
Note though – this will only be a problem if you have many people with words like van or de in their name.
Henry – good catch. What’s your solution?
Lol, probs for knowing many Dutch soccer players.
Well actually, the site I’m working on has 127 members and many of them have ‘van’, ‘de’, or ‘van der’ in their last name and the sorting is almost all fine. There are a few mis matches but that’s probably our own fault due to incorrect registration in the WordPress backend.
It also goes wrong when members have a double first name but I guess that also depends on the registration in the backend. But I still have to check this.
@shanebp Maybe just remove the first word from the string? i.e.
strstr( 'Robin van Persie', ' ' )
which gives van Persie
. Then you just need remove the initial space:
substr( ' van Persie', 1 )
@nielspilon if the sorting is fine (van, de etc) then that is down to luck 🙂
So handle potential secondary and tertiary sorting ? – yikes.
This can’t be the first time this issue has arisen.
A deep google should turn up something.
why wouldn’t secondary and tertiary sorting be taken care of by substr( strstr( u.display_name, ' ' ), 1 )
?
Checked in the backend and where it goes wrong is due to our own data entry.
@nielspilon I really do think substr( strstr( u.display_name, ' ' ), 1 )
is your answer. You say there’s definitely no members returned with that in place?