Sorting Member-Directory by Last Name, Custom Solution
-
Currently, I am supporting a Buddypress site that has the member-directory being sorted by last name, using this solution:
function aps_bp_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)"; } }
You can see the result here:
That implementation has a couple of problems.
(1) It doesn’t ignore special characters. I understand this is a MYSQL issue. Going to post-pone addressing it for the moment.
(2) It doesn’t conventionally sort compound last names. I’d like to sort ‘de Rose’ under ‘D’ and ‘Savage Upper’ under ‘S’.
To solve (2), I think I need to sort by wp_user_meta.last_name (or the corresponding xprofile field they have). I have tried both, and my implementations currently break bp_members_pagination_count() and bp_members_pagination_links(). Using the following code**, for example,
function aps_bp_alphabetize_by_last_name( $bp_user_query) { if ( 'alphabetical' == $bp_user_query->query_vars['type'] ){ $bp_user_query->uid_table = 'wp_usermeta'; $bp_user_query->uid_name = 'user_id'; $bp_user_query->uid_clauses['select'] = "SELECT u.{$bp_user_query-> uid_name} as id FROM {$bp_user_query->uid_table} u"; $bp_user_query->uid_clauses['where'] = "WHERE u.meta_key = 'last_name'"; $bp_user_query->uid_clauses['orderby'] = "ORDER BY u.meta_value"; } }
generates this: http://printscholars.staging.wpengine.com/member-directory/
** This is meant to be provisional. It’s not, for example, taking into account the include and exclude in the query vars, which I will need to do.
Any insight into what here is breaking those functions and other problems this approach might lead to?
Using WP 4.2.2,Buddypress 2.2.3.1
- The topic ‘Sorting Member-Directory by Last Name, Custom Solution’ is closed to new replies.