Hi @aaronkin
Did you actually manage to get your custom ‘sorts’ to actually sort the data or does it just filter it? I have managed to add a new option to ‘Order By’ which I can use to filter results, but it won’t sort the data by a profile field. It will only accept the current types, i.e. ‘alphabetical’ ‘last active’ etc.
Yes. I have it filter by User Role (wp_capabilities) then sort the list by username. I barely got this working and it took me forever. I would like to also Sort By a xprofile field but i dont know how. I’m sure this code gets me really close but i’m just not experienced enough to figure this out just yet.
here’s what i have in my functions.php
This will add 3 drop down items, Organizations, Funders, Company. this sorts by User Role. when people signup for my site they choose the appropriate Reg. Form (each form assigns user role).
If you ever find out HOW TO SORT BY xProfile Field, please post here.
add_action( 'bp_members_directory_order_options', 'add_sortby_role' );
function add_sortby_role() { ?>
<option value="sort-organization">Organization</option>
<option value="sort-funder">Funder</option>
<option value="sort-company">Company</option>
<?php
}
add_action( 'bp_pre_user_query', 'mysortbyorganization' );
add_action( 'bp_pre_user_query', 'mysortbyfunder' );
add_action( 'bp_pre_user_query', 'mysortbycompany' );
function mysortbyorganization( $BP_User_Query ) {
// Only run this if one of our custom options is selected
if ( in_array( $BP_User_Query->query_vars['type'], array( 'sort-organization' ) ) ) {
global $wpdb;
// Adjust SELECT
$BP_User_Query->uid_clauses['select'] = "
SELECT u.ID, u.user_login, u.user_nicename, u.user_email
FROM $wpdb->users u
INNER JOIN $wpdb->usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities'
AND m.meta_value LIKE '%organization%'
ORDER BY u.user_nicename
";
}
}
function mysortbyfunder( $BP_User_Query ) {
// Only run this if one of our custom options is selected
if ( in_array( $BP_User_Query->query_vars['type'], array( 'sort-funder' ) ) ) {
global $wpdb;
// Adjust SELECT
$BP_User_Query->uid_clauses['select'] = "
SELECT u.ID, u.user_login, u.user_nicename, u.user_email
FROM $wpdb->users u
INNER JOIN $wpdb->usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities'
AND m.meta_value LIKE '%funder%'
ORDER BY u.user_nicename
";
}
}
function mysortbycompany( $BP_User_Query ) {
// Only run this if one of our custom options is selected
if ( in_array( $BP_User_Query->query_vars['type'], array( 'sort-company' ) ) ) {
global $wpdb;
// Adjust SELECT
$BP_User_Query->uid_clauses['select'] = "
SELECT u.ID, u.user_login, u.user_nicename, u.user_email
FROM $wpdb->users u
INNER JOIN $wpdb->usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities'
AND m.meta_value LIKE '%company%'
ORDER BY u.user_nicename
";
}
}