Skip to:
Content
Pages
Categories
Search
Top
Bottom

Sorting Member-Directory by Last Name, Custom Solution

  • @gato-gordo

    Participant

    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:

    Member Directory

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • @shanebp

    Moderator

    @gato-gordo

    Participant

    Hey, thanks. That final solution sorts some more of the compound last names correctly, but takes people that have added a middle initial to their first name out of order. E.g., Linda C. Floyd gets sorted as a ‘C’.

    Does anyone know how to do this with the wp_usermeta.last_name or know why the code I posted breaks those functions?

    @imuhammad

    Participant

    // code for functions.php

    function xfield_member_filter( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
    
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id, value FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " ORDER BY value" ;
      else
       return '';
      // var_dump($query); die;
      if ( $field_value != '' ) 
        $query .= " AND value LIKE '%" . $field_value . "%'";
          /* 
          LIKE is slow. If you're sure the value has not been serialized, you can do this:
          $query .= " AND value = '" . $field_value . "'";
          */
      
      $custom_ids = $wpdb->get_col( $query );
      
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
       
    }
    
    function alphabetize_by_last_name( $bp_user_query ) {
      ;
      if ( 'alphabetical' == $bp_user_query->query_vars['type'] ) {
        $bp_user_query->uid_clauses['orderby'] = "ORDER BY FIELD(u.ID," . $bp_user_query->query_vars['include'] . ")";  
        $bp_user_query->uid_clauses['order'] = "";  
        var_dump($bp_user_query);
      }
    }
    add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );
    
    // code for loop
    <?php if ( bp_has_members( xfield_member_filter( 'Last Name' ).'&type=alphabetical' ) ) : ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sorting Member-Directory by Last Name, Custom Solution’ is closed to new replies.
Skip to toolbar