diff --git src/bp-core/bp-core-adminbar.php src/bp-core/bp-core-adminbar.php
index a875c49..eade1b6 100644
--- src/bp-core/bp-core-adminbar.php
+++ src/bp-core/bp-core-adminbar.php
@@ -127,4 +127,4 @@ function bp_core_enqueue_admin_bar_css() {
 
 	// Enqueue the additional adminbar css
 	wp_enqueue_style( 'bp-admin-bar' );
-}
\ No newline at end of file
+}
diff --git src/bp-core/bp-core-classes.php src/bp-core/bp-core-classes.php
index 1244cdf..c99c031 100644
--- src/bp-core/bp-core-classes.php
+++ src/bp-core/bp-core-classes.php
@@ -48,6 +48,8 @@ if ( !defined( 'ABSPATH' ) ) exit;
  *     @type string|bool $meta_value When used with $meta_key, limits results
  *           to users whose usermeta value associated with $meta_key matches
  *           $meta_value. Default: false.
+ *     @type array $xprofile_query Filter results by xprofile data. Requires
+ *           the xprofile component. See {@link BP_XProfile_Query} for details.
  *     @type bool $populate_extras True if you want to fetch extra metadata
  *           about returned users, such as total group and friend counts.
  *     @type string $count_total Determines how BP_User_Query will do a count
@@ -115,6 +117,15 @@ class BP_User_Query {
 	public $uid_clauses = array();
 
 	/**
+	 * SQL table where the user ID is being fetched from.
+	 *
+	 * @since BuddyPress (2.1.0)
+	 * @access public
+	 * @var string
+	 */
+	public $uid_table = '';
+
+	/**
 	 * SQL database column name to order by.
 	 *
 	 * @since BuddyPress (1.7.0)
@@ -162,6 +173,7 @@ class BP_User_Query {
 				'user_ids'        => false,
 				'meta_key'        => false,
 				'meta_value'      => false,
+				'xprofile_query'  => false,
 				'populate_extras' => true,
 				'count_total'     => 'count_query'
 			) );
@@ -249,7 +261,8 @@ class BP_User_Query {
 			// number of minutes used as an interval
 			case 'online' :
 				$this->uid_name = 'user_id';
-				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$bp->members->table_name_last_activity} u";
+				$this->uid_table = $bp->members->table_name_last_activity;
+				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
 				$sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );
 				$sql['where'][] = $wpdb->prepare( "u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) );
 				$sql['orderby'] = "ORDER BY u.date_recorded";
@@ -263,7 +276,8 @@ class BP_User_Query {
 			case 'newest' :
 			case 'random' :
 				$this->uid_name = 'user_id';
-				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$bp->members->table_name_last_activity} u";
+				$this->uid_table = $bp->members->table_name_last_activity;
+				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
 				$sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );
 
 				if ( 'newest' == $type ) {
@@ -281,7 +295,8 @@ class BP_User_Query {
 			// 'popular' sorts by the 'total_friend_count' usermeta
 			case 'popular' :
 				$this->uid_name = 'user_id';
-				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$wpdb->usermeta} u";
+				$this->uid_table = $wpdb->usermeta;
+				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
 				$sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) );
 				$sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)";
 				$sql['order']   = "DESC";
@@ -298,7 +313,8 @@ class BP_User_Query {
 				// @todo remove need for bp_is_active() check
 				if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) {
 					$this->uid_name = 'ID';
-					$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$wpdb->users} u";
+					$this->uid_table = $wpdb->users;
+					$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
 					$sql['orderby'] = "ORDER BY u.display_name";
 					$sql['order']   = "ASC";
 
@@ -306,7 +322,8 @@ class BP_User_Query {
 				// the xprofile table
 				} else {
 					$this->uid_name = 'user_id';
-					$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$bp->profile->table_name_data} u";
+					$this->uid_table = $bp->profile->table_name_data;
+					$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
 					$sql['where'][] = $wpdb->prepare( "u.field_id = %d", bp_xprofile_fullname_field_id() );
 					$sql['orderby'] = "ORDER BY u.value";
 					$sql['order']   = "ASC";
@@ -322,7 +339,8 @@ class BP_User_Query {
 			// Any other 'type' falls through
 			default :
 				$this->uid_name = 'ID';
-				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$wpdb->users} u";
+				$this->uid_table = $wpdb->users;
+				$sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
 
 				// In this case, we assume that a plugin is
 				// handling order, so we leave those clauses
diff --git src/bp-xprofile/bp-xprofile-classes.php src/bp-xprofile/bp-xprofile-classes.php
index cbb1b8b4..ed08b9b 100644
--- src/bp-xprofile/bp-xprofile-classes.php
+++ src/bp-xprofile/bp-xprofile-classes.php
@@ -3262,3 +3262,527 @@ abstract class BP_XProfile_Field_Type {
 		return $html;
 	}
 }
+
+/**
+ * Class for generating SQL clauses to filter a user query by xprofile data.
+ *
+ * @since BuddyPress (2.2.0)
+ */
+class BP_XProfile_Query {
+	/**
+	 * Array of xprofile queries.
+	 *
+	 * See {@see WP_XProfile_Query::__construct()} for information on parameters.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 * @var array
+	 */
+	public $queries = array();
+
+	/**
+	 * Database table that where the metadata's objects are stored (eg $wpdb->users).
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 * @var string
+	 */
+	public $primary_table;
+
+	/**
+	 * Column in primary_table that represents the ID of the object.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 * @var string
+	 */
+	public $primary_id_column;
+
+	/**
+	 * A flat list of table aliases used in JOIN clauses.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access protected
+	 * @var array
+	 */
+	protected $table_aliases = array();
+	public function __construct( $xprofile_query ) {
+		if ( empty( $xprofile_query ) ) {
+			return;
+		}
+
+		$this->queries = $this->sanitize_query( $xprofile_query );
+	}
+
+	/**
+	 * Ensure the `xprofile_query` argument passed to the class constructor is well-formed.
+	 *
+	 * Eliminates empty items and ensures that a 'relation' is set.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 *
+	 * @param array $queries Array of query clauses.
+	 * @return array Sanitized array of query clauses.
+	 */
+	public function sanitize_query( $queries ) {
+		$clean_queries = array();
+
+		if ( ! is_array( $queries ) ) {
+			return $clean_queries;
+		}
+
+		foreach ( $queries as $key => $query ) {
+			if ( 'relation' === $key ) {
+				$relation = $query;
+
+			} else if ( ! is_array( $query ) ) {
+				continue;
+
+			// First-order clause.
+			} else if ( $this->is_first_order_clause( $query ) ) {
+				if ( isset( $query['value'] ) && array() === $query['value'] ) {
+					unset( $query['value'] );
+				}
+
+				$clean_queries[] = $query;
+
+			// Otherwise, it's a nested query, so we recurse.
+			} else {
+				$cleaned_query = $this->sanitize_query( $query );
+
+				if ( ! empty( $cleaned_query ) ) {
+					$clean_queries[] = $cleaned_query;
+				}
+			}
+		}
+
+		if ( empty( $clean_queries ) ) {
+			return $clean_queries;
+		}
+
+		// Sanitize the 'relation' key provided in the query.
+		if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
+			$clean_queries['relation'] = 'OR';
+
+		/*
+		 * If there is only a single clause, call the relation 'OR'.
+		 * This value will not actually be used to join clauses, but it
+		 * simplifies the logic around combining key-only queries.
+		 */
+		} else if ( 1 === count( $clean_queries ) ) {
+			$clean_queries['relation'] = 'OR';
+
+		// Default to AND.
+		} else {
+			$clean_queries['relation'] = 'AND';
+		}
+
+		return $clean_queries;
+	}
+
+	/**
+	 * Determine whether a query clause is first-order.
+	 *
+	 * A first-order meta query clause is one that has either a 'key' or
+	 * a 'value' array key.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access protected
+	 *
+	 * @param array $query Meta query arguments.
+	 * @return bool Whether the query clause is a first-order clause.
+	 */
+	protected function is_first_order_clause( $query ) {
+		return isset( $query['field'] ) || isset( $query['value'] );
+	}
+
+	/**
+	 * Return the appropriate alias for the given meta type if applicable.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 *
+	 * @param string $type MySQL type to cast meta_value.
+	 * @return string MySQL type.
+	 */
+	public function get_cast_for_type( $type = '' ) {
+		if ( empty( $type ) )
+			return 'CHAR';
+
+		$meta_type = strtoupper( $type );
+
+		if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
+			return 'CHAR';
+
+		if ( 'NUMERIC' == $meta_type )
+			$meta_type = 'SIGNED';
+
+		return $meta_type;
+	}
+
+	/**
+	 * Generate SQL clauses to be appended to a main query.
+	 *
+	 * Called by the public {@see WP_Meta_Query::get_sql()}, this method
+	 * is abstracted out to maintain parity with the other Query classes.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access protected
+	 *
+	 * @return array {
+	 *     Array containing JOIN and WHERE SQL clauses to append to the main query.
+	 *
+	 *     @type string $join  SQL fragment to append to the main JOIN clause.
+	 *     @type string $where SQL fragment to append to the main WHERE clause.
+	 * }
+	 */
+	protected function get_sql_clauses() {
+		/*
+		 * $queries are passed by reference to get_sql_for_query() for recursion.
+		 * To keep $this->queries unaltered, pass a copy.
+		 */
+		$queries = $this->queries;
+		$sql = $this->get_sql_for_query( $queries );
+
+		if ( ! empty( $sql['where'] ) ) {
+			$sql['where'] = ' AND ' . $sql['where'];
+		}
+
+		return $sql;
+	}
+
+	/**
+	 * Generate SQL clauses for a single query array.
+	 *
+	 * If nested subqueries are found, this method recurses the tree to
+	 * produce the properly nested SQL.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access protected
+	 *
+	 * @param array $query Query to parse.
+	 * @param int   $depth Optional. Number of tree levels deep we currently are.
+	 *               Used to calculate indentation.
+	 * @return array {
+	 *     Array containing JOIN and WHERE SQL clauses to append to a single query array.
+	 *
+	 *     @type string $join  SQL fragment to append to the main JOIN clause.
+	 *     @type string $where SQL fragment to append to the main WHERE clause.
+	 * }
+	 */
+	protected function get_sql_for_query( &$query, $depth = 0 ) {
+		$sql_chunks = array(
+			'join'  => array(),
+			'where' => array(),
+		);
+
+		$sql = array(
+			'join'  => '',
+			'where' => '',
+		);
+
+		$indent = '';
+		for ( $i = 0; $i < $depth; $i++ ) {
+			$indent .= "  ";
+		}
+
+		foreach ( $query as $key => &$clause ) {
+			if ( 'relation' === $key ) {
+				$relation = $query['relation'];
+			} else if ( is_array( $clause ) ) {
+
+				// This is a first-order clause.
+				if ( $this->is_first_order_clause( $clause ) ) {
+					$clause_sql = $this->get_sql_for_clause( $clause, $query );
+
+					$where_count = count( $clause_sql['where'] );
+					if ( ! $where_count ) {
+						$sql_chunks['where'][] = '';
+					} else if ( 1 === $where_count ) {
+						$sql_chunks['where'][] = $clause_sql['where'][0];
+					} else {
+						$sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
+					}
+
+					$sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
+				// This is a subquery, so we recurse.
+				} else {
+					$clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
+
+					$sql_chunks['where'][] = $clause_sql['where'];
+					$sql_chunks['join'][]  = $clause_sql['join'];
+				}
+			}
+		}
+
+		// Filter to remove empties.
+		$sql_chunks['join']  = array_filter( $sql_chunks['join'] );
+		$sql_chunks['where'] = array_filter( $sql_chunks['where'] );
+
+		if ( empty( $relation ) ) {
+			$relation = 'AND';
+		}
+
+		// Filter duplicate JOIN clauses and combine into a single string.
+		if ( ! empty( $sql_chunks['join'] ) ) {
+			$sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
+		}
+
+		// Generate a single WHERE clause with proper brackets and indentation.
+		if ( ! empty( $sql_chunks['where'] ) ) {
+			$sql['where'] = '( ' . "\n  " . $indent . implode( ' ' . "\n  " . $indent . $relation . ' ' . "\n  " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
+		}
+
+		return $sql;
+	}
+
+	/**
+	 * Generates SQL clauses to be appended to a main query.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 *
+	 * @param string $primary_table     Database table where the object being filtered is stored (eg wp_users).
+	 * @param string $primary_id_column ID column for the filtered object in $primary_table.
+	 * @return array {
+	 *     Array containing JOIN and WHERE SQL clauses to append to the main query.
+	 *
+	 *     @type string $join  SQL fragment to append to the main JOIN clause.
+	 *     @type string $where SQL fragment to append to the main WHERE clause.
+	 * }
+	 */
+	public function get_sql( $primary_table, $primary_id_column, $context = null ) {
+		global $wpdb;
+
+		$this->primary_table     = $primary_table;
+		$this->primary_id_column = $primary_id_column;
+
+		$sql = $this->get_sql_clauses();
+
+		/*
+		 * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
+		 * be LEFT. Otherwise posts with no metadata will be excluded from results.
+		 */
+		if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
+			$sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
+		}
+
+		/**
+		 * Filter the meta query's generated SQL.
+		 *
+		 * @since 2.1.0
+		 *
+		 * @param array $args {
+		 *     An array of meta query SQL arguments.
+		 *
+		 *     @type array  $clauses           Array containing the query's JOIN and WHERE clauses.
+		 *     @type array  $queries           Array of meta queries.
+		 *     @type string $primary_table     Primary table.
+		 *     @type string $primary_id_column Primary column ID.
+		 * }
+		 */
+		return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $primary_table, $primary_id_column ) );
+	}
+
+	/**
+	 * Generate SQL JOIN and WHERE clauses for a first-order query clause.
+	 *
+	 * "First-order" means that it's an array with a 'key' or 'value'.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access public
+	 *
+	 * @param array $clause       Query clause.
+	 * @param array $parent_query Parent query array.
+	 * @return array {
+	 *     Array containing JOIN and WHERE SQL clauses to append to a first-order query.
+	 *
+	 *     @type string $join  SQL fragment to append to the main JOIN clause.
+	 *     @type string $where SQL fragment to append to the main WHERE clause.
+	 * }
+	 */
+	public function get_sql_for_clause( &$clause, $parent_query ) {
+		global $wpdb;
+
+		$sql_chunks = array(
+			'where' => array(),
+			'join' => array(),
+		);
+
+		if ( isset( $clause['compare'] ) ) {
+			$clause['compare'] = strtoupper( $clause['compare'] );
+		} else {
+			$clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
+		}
+
+		if ( ! in_array( $clause['compare'], array(
+			'=', '!=', '>', '>=', '<', '<=',
+			'LIKE', 'NOT LIKE',
+			'IN', 'NOT IN',
+			'BETWEEN', 'NOT BETWEEN',
+			'EXISTS', 'NOT EXISTS',
+			'REGEXP', 'NOT REGEXP', 'RLIKE'
+		) ) ) {
+			$clause['compare'] = '=';
+		}
+
+		$field_compare = $clause['compare'];
+
+		// First build the JOIN clause, if one is required.
+		$join = '';
+
+		$data_table = buddypress()->profile->table_name_data;
+
+		// We prefer to avoid joins if possible. Look for an existing join compatible with this clause.
+		$alias = $this->find_compatible_table_alias( $clause, $parent_query );
+		if ( false === $alias ) {
+			$i = count( $this->table_aliases );
+			$alias = $i ? 'xpq' . $i : $data_table;
+
+			// JOIN clauses for NOT EXISTS have their own syntax.
+			if ( 'NOT EXISTS' === $field_compare ) {
+				$join .= " LEFT JOIN $data_table";
+				$join .= $i ? " AS $alias" : '';
+				$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.user_id AND $alias.field_id = %d )", $clause['field'] );
+
+			// All other JOIN clauses.
+			} else {
+				$join .= " INNER JOIN $data_table";
+				$join .= $i ? " AS $alias" : '';
+				$join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.user_id )";
+			}
+
+			$this->table_aliases[] = $alias;
+			$sql_chunks['join'][] = $join;
+		}
+
+		// Save the alias to this clause, for future siblings to find.
+		$clause['alias'] = $alias;
+
+		// Next, build the WHERE clause.
+		$where = '';
+
+		// field_id.
+		if ( array_key_exists( 'field', $clause ) ) {
+			// Convert field name to ID if necessary.
+			if ( ! is_numeric( $clause['field'] ) ) {
+				$clause['field'] = xprofile_get_field_id_from_name( $clause['field'] );
+			}
+
+			// NOT EXISTS has its own syntax.
+			if ( 'NOT EXISTS' === $field_compare ) {
+				$sql_chunks['where'][] = $alias . '.user_id IS NULL';
+			} else {
+				$sql_chunks['where'][] = $wpdb->prepare( "$alias.field_id = %d", $clause['field'] );
+			}
+		}
+
+		// value.
+		if ( array_key_exists( 'value', $clause ) ) {
+			$field_value = $clause['value'];
+			$field_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
+
+			if ( in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
+				if ( ! is_array( $field_value ) ) {
+					$field_value = preg_split( '/[,\s]+/', $field_value );
+				}
+			} else {
+				$field_value = trim( $field_value );
+			}
+
+			switch ( $field_compare ) {
+				case 'IN' :
+				case 'NOT IN' :
+					$field_compare_string = '(' . substr( str_repeat( ',%s', count( $field_value ) ), 1 ) . ')';
+					$where = $wpdb->prepare( $field_compare_string, $field_value );
+					break;
+
+				case 'BETWEEN' :
+				case 'NOT BETWEEN' :
+					$field_value = array_slice( $field_value, 0, 2 );
+					$where = $wpdb->prepare( '%s AND %s', $field_value );
+					break;
+
+				case 'LIKE' :
+				case 'NOT LIKE' :
+					$field_value = '%' . $wpdb->esc_like( $field_value ) . '%';
+					$where = $wpdb->prepare( '%s', $field_value );
+					break;
+
+				default :
+					$where = $wpdb->prepare( '%s', $field_value );
+					break;
+
+			}
+
+			if ( $where ) {
+				$sql_chunks['where'][] = "CAST($alias.value AS {$field_type}) {$field_compare} {$where}";
+			}
+		}
+
+		/*
+		 * Multiple WHERE clauses (for meta_key and meta_value) should
+		 * be joined in parentheses.
+		 */
+		if ( 1 < count( $sql_chunks['where'] ) ) {
+			$sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' );
+		}
+
+		return $sql_chunks;
+	}
+
+	/**
+	 * Identify an existing table alias that is compatible with the current query clause.
+	 *
+	 * We avoid unnecessary table joins by allowing each clause to look for
+	 * an existing table alias that is compatible with the query that it
+	 * needs to perform. An existing alias is compatible if (a) it is a
+	 * sibling of $clause (ie, it's under the scope of the same relation),
+	 * and (b) the combination of operator and relation between the clauses
+	 * allows for a shared table join. In the case of WP_Meta_Query, this
+	 * only applies to IN clauses that are connected by the relation OR.
+	 *
+	 * @since BuddyPress (2.2.0)
+	 * @access protected
+	 *
+	 * @param  array       $clause       Query clause.
+	 * @param  array       $parent_query Parent query of $clause.
+	 * @return string|bool Table alias if found, otherwise false.
+	 */
+	protected function find_compatible_table_alias( $clause, $parent_query ) {
+		$alias = false;
+
+		foreach ( $parent_query as $sibling ) {
+			// If the sibling has no alias yet, there's nothing to check.
+			if ( empty( $sibling['alias'] ) ) {
+				continue;
+			}
+
+			// We're only interested in siblings that are first-order clauses.
+			if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
+				continue;
+			}
+
+			$compatible_compares = array();
+
+			// Clauses connected by OR can share joins as long as they have "positive" operators.
+			if ( 'OR' === $parent_query['relation'] ) {
+				$compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' );
+
+			// Clauses joined by AND with "negative" operators share a join only if they also share a key.
+			} else if ( isset( $sibling['field_id'] ) && isset( $clause['field_id'] ) && $sibling['field_id'] === $clause['field_id'] ) {
+				$compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' );
+			}
+
+			$clause_compare  = strtoupper( $clause['compare'] );
+			$sibling_compare = strtoupper( $sibling['compare'] );
+			if ( in_array( $clause_compare, $compatible_compares ) && in_array( $sibling_compare, $compatible_compares ) ) {
+				$alias = $sibling['alias'];
+				break;
+			}
+		}
+
+		return $alias;
+	}
+}
diff --git src/bp-xprofile/bp-xprofile-filters.php src/bp-xprofile/bp-xprofile-filters.php
index 21eaa3d..411a8d4 100644
--- src/bp-xprofile/bp-xprofile-filters.php
+++ src/bp-xprofile/bp-xprofile-filters.php
@@ -200,7 +200,7 @@ function xprofile_filter_pre_validate_value_by_field_type( $value, $field, $fiel
 /**
  * Filter an Extended Profile field value, and attempt to make clickable links
  * to members search results out of them.
- * 
+ *
  * - Not run on datebox field types
  * - Not run on values without commas with less than 5 words
  * - URL's are made clickable
@@ -323,6 +323,30 @@ function bp_xprofile_filter_user_query_populate_extras( BP_User_Query $user_quer
 add_filter( 'bp_user_query_populate_extras', 'bp_xprofile_filter_user_query_populate_extras', 2, 2 );
 
 /**
+ * Parse 'xprofile_query' argument passed to BP_User_Query.
+ *
+ * @since BuddyPress (2.2.0)
+ *
+ * @param BP_User_Query User query object.
+ */
+function bp_xprofile_add_xprofile_query_to_user_query( BP_User_Query $q ) {
+	global $wpdb;
+
+	if ( empty( $q->query_vars['xprofile_query'] ) ) {
+		return;
+	}
+
+	$xprofile_query = new BP_XProfile_Query( $q->query_vars['xprofile_query'] );
+	$sql = $xprofile_query->get_sql( 'u', $q->uid_name );
+
+	if ( ! empty( $sql['join'] ) ) {
+		$q->uid_clauses['select'] .= $sql['join'];
+		$q->uid_clauses['where'] .= $sql['where'];
+	}
+}
+add_action( 'bp_pre_user_query', 'bp_xprofile_add_xprofile_query_to_user_query' );
+
+/**
  * Filter meta queries to modify for the xprofile data schema.
  *
  * @since BuddyPress (2.0.0)
diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-query.php tests/phpunit/testcases/xprofile/class-bp-xprofile-query.php
new file mode 100644
index 0000000..2f3f751
--- /dev/null
+++ tests/phpunit/testcases/xprofile/class-bp-xprofile-query.php
@@ -0,0 +1,586 @@
+<?php
+
+/**
+ * @group xprofile
+ * @group BP_XProfile_Query
+ */
+class BP_Tests_BP_XProfile_Query extends BP_UnitTestCase {
+	protected $group;
+	protected $fields = array();
+	protected $users = array();
+
+	public function tearDown() {
+		parent::tearDown();
+		$this->group = '';
+		$this->fields = array();
+	}
+
+	public function test_no_field() {
+		$this->create_fields( 2 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'value' => 'foo',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0], $this->users[2] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_no_value() {
+		$this->create_fields( 2 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[1],
+				),
+			),
+		) );
+
+		$expected = array( $this->users[2] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_translate_field_name_to_field_id() {
+		$this->create_fields( 0 );
+		$f = $this->factory->xprofile_field->create( array(
+			'field_group_id' => $this->group,
+			'type' => 'textbox',
+			'name' => 'Foo Field',
+		) );
+		$this->create_users( 2 );
+
+		xprofile_set_field_data( $f, $this->users[0], 'foo' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => 'Foo Field',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_default() {
+		$this->create_fields( 2 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'foo',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0] );
+		$this->assertEquals( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_equals() {
+		$this->create_fields( 2 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo');
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'foo',
+					'compare' => '=',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0] );
+		$this->assertEquals( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_not_equals() {
+		$this->create_fields( 1 );
+		$this->create_users( 2 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'foo',
+					'compare' => '!=',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1] );
+		$this->assertEquals( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_arithmetic_comparisons() {
+		$this->create_fields( 1 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], '1' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], '2' );
+		xprofile_set_field_data( $this->fields[0], $this->users[2], '3' );
+
+		// <
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 2,
+					'compare' => '<',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+
+		// <=
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 2,
+					'compare' => '<=',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0], $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+
+		// >=
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 2,
+					'compare' => '>=',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1], $this->users[2] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+
+		// >
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 2,
+					'compare' => '>',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[2] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_like() {
+		$this->create_fields( 1 );
+		$this->create_users( 2 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'ba',
+					'compare' => 'LIKE',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_not_like() {
+		$this->create_fields( 1 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'rab' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'ba',
+					'compare' => 'NOT LIKE',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_between_not_between() {
+		$this->create_fields( 1 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], '1' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], '10' );
+		xprofile_set_field_data( $this->fields[0], $this->users[2], '100' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => array( 9, 12 ),
+					'compare' => 'BETWEEN',
+					'type' => 'NUMERIC',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => array( 9, 12 ),
+					'compare' => 'NOT BETWEEN',
+					'type' => 'NUMERIC',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0], $this->users[2] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_regexp_rlike() {
+		$this->create_fields( 1 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'baz' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'z$',
+					'compare' => 'REGEXP',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+
+		// RLIKE is a synonym for REGEXP.
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'z$',
+					'compare' => 'RLIKE',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_not_regexp() {
+		$this->create_fields( 1 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'baz' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'z$',
+					'compare' => 'NOT REGEXP',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_single_clause_compare_not_exists() {
+		$this->create_fields( 2 );
+		$this->create_users( 2 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[1], 'bar' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'compare' => 'NOT EXISTS',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_relation_default_to_and() {
+		$this->create_fields( 2 );
+		$this->create_users( 4 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[3], 'foo' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				array(
+					'field' => $this->fields[0],
+					'value' => 'foo',
+				),
+				array(
+					'field' => $this->fields[1],
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[3] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_relation_and() {
+		$this->create_fields( 2 );
+		$this->create_users( 4 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[3], 'foo' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				'relation' => 'AND',
+				array(
+					'field' => $this->fields[0],
+					'value' => 'foo',
+				),
+				array(
+					'field' => $this->fields[1],
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[3] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_relation_or() {
+		$this->create_fields( 2 );
+		$this->create_users( 4 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[3], 'foo' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				'relation' => 'OR',
+				array(
+					'field' => $this->fields[0],
+					'value' => 'foo',
+				),
+				array(
+					'field' => $this->fields[1],
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0], $this->users[3] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_relation_and_with_compare_not_exists() {
+		$this->create_fields( 2 );
+		$this->create_users( 4 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				'relation' => 'AND',
+				array(
+					'field' => $this->fields[0],
+					'compare' => 'NOT EXISTS',
+				),
+				array(
+					'field' => $this->fields[1],
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[3] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_relation_or_with_compare_not_exists() {
+		$this->create_fields( 2 );
+		$this->create_users( 4 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[3], 'bar' );
+		xprofile_set_field_data( $this->fields[0], $this->users[3], 'bar' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				'relation' => 'OR',
+				array(
+					'field' => $this->fields[0],
+					'compare' => 'NOT EXISTS',
+				),
+				array(
+					'field' => $this->fields[1],
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[2], $this->users[3] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	/**
+	 * Tests for table join logic.
+	 */
+	public function test_relation_or_compare_equals_and_like() {
+		$this->create_fields( 2 );
+		$this->create_users( 4 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[3], 'barry' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				'relation' => 'OR',
+				array(
+					'field' => $this->fields[0],
+					'compare' => '=',
+					'value' => 'foo',
+				),
+				array(
+					'field' => $this->fields[1],
+					'value' => 'bar',
+					'compare' => 'LIKE',
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0], $this->users[3] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	public function test_nested() {
+		$this->create_fields( 2 );
+		$this->create_users( 3 );
+
+		xprofile_set_field_data( $this->fields[0], $this->users[0], 'foo' );
+		xprofile_set_field_data( $this->fields[0], $this->users[1], 'bar' );
+		xprofile_set_field_data( $this->fields[1], $this->users[2], 'foo' );
+		xprofile_set_field_data( $this->fields[1], $this->users[1], 'foo' );
+
+		$q = new BP_User_Query( array(
+			'xprofile_query' => array(
+				'relation' => 'OR',
+				array(
+					'field' => $this->fields[0],
+					'compare' => '=',
+					'value' => 'foo',
+				),
+				array(
+					'relation' => 'AND',
+					array(
+						'field' => $this->fields[0],
+						'value' => 'bar',
+					),
+					array(
+						'field' => $this->fields[1],
+						'value' => 'foo',
+					),
+				),
+			),
+		) );
+
+		$expected = array( $this->users[0], $this->users[1] );
+		$this->assertEqualSets( $expected, array_keys( $q->results ) );
+	}
+
+	/** Helpers **********************************************************/
+
+	protected function create_fields( $count ) {
+		$this->group = $this->factory->xprofile_group->create();
+		for ( $i = 0; $i < $count; $i++ ) {
+			$this->fields[] = $this->factory->xprofile_field->create( array(
+				'field_group_id' => $this->group,
+				'type' => 'textbox',
+			) );
+		}
+	}
+
+	protected function create_users( $count ) {
+		for ( $i = 0; $i < $count; $i++ ) {
+			$this->users[] = $this->create_user();
+		}
+	}
+}
