Skip to:

Sorting member directory by display name not username

  • terraling


    Hi, I have a problem with the WordPress – BuddyPress profile sync.

    In my members directory using alpha sort the names are showing up based upon username rather than display name.

    Digging into the code to see how to fix that I see in bp-core-classes.php the following as part of function prepare_user_ids_query():

    // We prefer to do alphabetical sorts against the display_name field
    // of wp_users, because the table is smaller and better indexed. We
    // can do so if xprofile sync is enabled, or if xprofile is inactive.

    In the settings I have xprofile sync enabled, but I’m not sure what it is supposed to do, or how. What it is NOT doing in my case is making any changes to the WordPress user table such as nickname to match up with the xprofile field for display name.

    I have a custom registration page so it may be that I’m not doing something that the default registration page does.

    Any idea what the likely omission may be?

    For the time being I’ve turned off syncing and so my members directory is correctly displaying in alpha order based on display name, but from the code it is clear that it would be more efficient to have sync enabled.

Viewing 2 replies - 1 through 2 (of 2 total)

  • godavid33


    I have something like the following for creating a custom sort. You can likely use the same thing for editing an existing sort:

    add_action( 'bp_pre_user_query', 'custom_pro_pre_user_query' );
    function custom_pro_pre_user_query( $BP_User_Query ) {
    	// Only run this if one of our custom options is selected
    	if ( in_array( $BP_User_Query->query_vars['type'], array( '/*Change this to the value of the Option in the sort drop-down needed*/' ) ) ) {
    		global $wpdb;
    		// Adjust SELECT
    		$BP_User_Query->uid_clauses['select'] = "
    			select distinct um.user_id, um.meta_value as latitude, um2.meta_value as longitude
    			from wp_usermeta as um
    			left join wp_usermeta as um2 on um.user_id=um2.user_id and um2.meta_key = 'geo_long'
    		// Adjust WHERE
    		$BP_User_Query->uid_clauses['where'] = "WHERE um.meta_key='geo_lat'";
    		// Adjust ORDER
    		$BP_User_Query->uid_clauses['order'] = ( $BP_User_Query->query_vars['type'] == 'points-asc' ) ? 'ASC' : 'DESC';
    		echo '<script>console.log("'.$BP_User_Query->uid_clauses['where'].'");</script>';

    I haven’t tested it for what you are asking, but I believe it should work. You’ll have to edit the MySQL query itself obviously to return the results you want. By the way I found this code from the MyCred plugin Author, Gabriel Merovingi (may have gotten spelling of last name wrong)



    Thanks @godavid33.

    I’d like to fix this without having to resort to a custom sql query.

    BuddyPress is designed to do this (sort alphabetically by display name rather than username) correctly.

    In the relevant code, it checks to see if WordPress-BuddyPress profile syncing is enabled. If it is, it runs the query against the wp_users table rather than the wp_bp_xprofile_data table, because “the table is smaller and better indexed”. If syncing is disabled then it runs the query against the xprofile display_name field instead.

    I have fixed the problem by turning off syncing so that it uses the xprofile display name, so my question is, what does profile syncing do exactly and when/how does it do it?

    I presume it overwrites nicename in wp_users with the display_name from xprofile (anything else?), but in my case it is not doing so, it appears broken.

    If I could identify the when/how then I’d be able to see if it is something I’ve broken or omitted in, for example, my custom registration page.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sorting member directory by display name not username’ is closed to new replies.
Skip to toolbar