Category Archives: BuddyPress

BuddyPress: Query Users by xProfile Data

Today I had a needed to query BuddyPress users by their extended profile data. I couldn’t find any functions in BuddyPress core that facilitate this, so I wrote my own. Here it is for anyone else who is looking for it:

 * Get users by BuddyPress xprofile data.
 * @param int    $field_id The ID of the field to search in.
 * @param string $value    The value to search for.
 * @return int[] The IDs of the users matching the search.
function my_bp_get_users_by_xprofile( $field_id, $value ) {

	global $wpdb;

    $user_ids = $wpdb->get_col(
    			SELECT `user_id`
    			FROM `{$wpdb->prefix}bp_xprofile_data`
    			WHERE `field_id` = %d
    				AND `value` = %s
    		, $field_id
    		, $value


$user_ids = my_bp_get_users_by_xprofile( 5, 'find me' );

If you need to get the field ID, you can use xprofile_get_field_id_from_name( 'field name' ).