Skip to:
Content
Pages
Categories
Search
Top
Bottom

Sort member directory by xprofile field


  • eberger3
    Participant

    @eberger3

    I’m helping implement some customizations on a BP site that I didn’t initially create. It is an alumni directory and there is a extended profile field for “Last Name at Graduation”. The ID on this field is 3.

    On the directory page there is select box to change the sorting of the results. They want it so the “Alphabetical” option sorts people by “Last Name at Graduation” because this is how people would likely look for you. It took me a while to get this far and I mostly used this tutorial.

    add_action( 'bp_pre_user_query', 'sort_by_ln_grad' );
    function sort_by_ln_grad( $BP_User_Query ) {
    	// Only run this if one of our custom options is selected
    	if ( in_array( $BP_User_Query->query_vars['type'], array( 'alphabetical') ) ) {
    		global $wpdb;
    
    		// Adjust SELECT
    		$BP_User_Query->uid_clauses['select'] = "
    			SELECT wp_efequx_users.id
                            FROM wp_efequx_users 
                            LEFT JOIN wp_efequx_bp_xprofile_data ON (wp_efequx_bp_xprofile_data.user_id                    
                            = wp_efequx_users.ID)";
    
    		// Adjust WHERE
    		$BP_User_Query->uid_clauses['where'] = "WHERE wp_efequx_bp_xprofile_data.field_id =  3 ";
    
    		// Adjust ORDER BY
    	        $BP_User_Query->uid_clauses['orderby'] = "ORDER by wp_efequx_bp_xprofile_data.value";	
    	}
    }

    and it works…sort of. When Alphabetical is selected the people are sorted by the desired field but there are issues:

    • The pagination doesn’t work
    • The member count shows ‘Viewing 1 member’ when there are many more
    • The “search members” box doesn’t work. It returns the entire directory

    Am I doing this the right way? How can I sort by this xprofile field without losing the other features of the member directory?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort member directory by xprofile field’ is closed to new replies.
Skip to toolbar