bp_has_members with custom fields [closed]

Buddypress’ bp_has_members() function accepts several parameters to alter the output. Take a look at the Accepted Parameters section on the codex page regarding the members loop. The two parameter meta_key and meta_value might just be what you’re looking for. The Code Examples section should give you an additional insight in how to do it – correct.


Update:

Firstly, you can’t use the »not equal | !=« operator like you’ve done in your comment.

Secondly, @MarutiMohanty might be right about the use of the correct values for the parameters. You might want to debug your usermeta, you can use below code to output this information on your members loop page – it goes into your functions.php.

Code:

add_action( 'bp_before_members_loop', 'wpse129106_usermeta_debug' );
function wpse129106_usermeta_debug() {
    $user_id = get_current_user_id();
    $all_meta_for_user = get_user_meta( $user_id );
    echo '<pre>';
    print_r( $all_meta_for_user );
    echo '</pre>';
}

Thirdly, after giving this situation a really quick test, I can say that all the below shown approaches are just working fine for me.

Code:

// query string, only key - works
$qs_args_key = 'per_page=0&meta_key=uniquekey';

// query string, key and value - works
$qs_args_key_and_value="per_page=0&meta_key=uniquekey&meta_value=uniquevalue";

// arguments array, key and value - works
$args_array = array(
    'per_page' => 0,
    'meta_key' => 'uniquekey',
    'meta_value' => 'uniquevalue'
);

// replace the variable to test the options
if ( bp_has_members( $args_array ) ) : 

2nd Update:

As you want to have the ability to compare the meta value you have do some additional work. This is pretty much following the pattern the buddypress codex example I referred you to above suggests.
In short, read the explanation at the codex page for details, we create a function that does the comparing and filters the ids accordingly, with that we make use of the include parameter the bp_has_members() has. I adapted the suggested example a bit, firstly by making a third argument – $compare – possible and secondly by changing up the conditional responsible for the SQL statement, that handles the comparing option. The code goes into your functions.php or bp-custom.php.

Code:

function wpse129106_bp_member_ids_by_field( $field_name, $field_value="", $compare="=" ) {
    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 FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
    else
        return '';

    if ( !empty( $query ) ) {    
        if ( $compare == 'LIKE' || $compare == 'NOT LIKE' ) {
            $query .= " AND value " . $compare . " '%" . $field_value . "%'";
        } else if ( $compare == '=' && $field_value == '' ) {
            $query = "SELECT DISTINCT user_id FROM " . $wpdb->prefix . "bp_xprofile_data";
            $query .= " WHERE user_id NOT IN";
            $query .= " (SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " AND value != '')";
        } else {
            $query .= " AND value " . $compare . " '" . $field_value . "'";
        }
    } else {
        return '';
    }

    $custom_ids = $wpdb->get_col( $query );

    if ( !empty( $custom_ids ) ) {
        $custom_ids_str="include=" . implode(",", $custom_ids);
        return $custom_ids_str;
    } else {
        return ''; 
    }
}

You can use it like in this example to alter your members loop:

if ( bp_has_members( 
         'per_page=0&' . 
         wpse129106_bp_member_ids_by_field(
             'First Name',
             'yourvalue',
             '!=' 
         ) 
     ) 
   ) :