Index: wp-includes/user.php
===================================================================
--- wp-includes/user.php	(revision 17693)
+++ wp-includes/user.php	(working copy)
@@ -501,8 +501,6 @@
 			$qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
 		}
 
-		_parse_meta_query( $qv );
-
 		$role = trim( $qv['role'] );
 
 		if ( $blog_id && ( $role || is_multisite() ) ) {
@@ -517,8 +515,11 @@
 			$qv['meta_query'][] = $cap_meta_query;
 		}
 
-		if ( !empty( $qv['meta_query'] ) ) {
-			$clauses = call_user_func_array( '_get_meta_sql', array( $qv['meta_query'], 'user', $wpdb->users, 'ID', &$this ) );
+		$meta_query = new WP_Meta_Query();
+		$meta_query->parse_query_vars( $qv );
+
+		if ( !empty( $meta_query->queries ) ) {
+			$clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
 			$this->query_from .= $clauses['join'];
 			$this->query_where .= $clauses['where'];
 		}
Index: wp-includes/query.php
===================================================================
--- wp-includes/query.php	(revision 17693)
+++ wp-includes/query.php	(working copy)
@@ -849,6 +849,15 @@
 	var $tax_query;
 
 	/**
+	 * Metadata query container
+	 *
+	 * @since 3.2
+	 * @access public
+	 * @var object WP_Meta_Query
+	 */
+	var $meta_query = false;
+
+	/**
 	 * Holds the data for a single object that is queried.
 	 *
 	 * Holds the contents of a post, page, category, attachment.
@@ -1525,8 +1534,6 @@
 			}
 			unset( $tax_query );
 
-			_parse_meta_query( $qv );
-
 			if ( empty($qv['author']) || ($qv['author'] == '0') ) {
 				$this->is_author = false;
 			} else {
@@ -1900,6 +1907,10 @@
 		// Fill again in case pre_get_posts unset some vars.
 		$q = $this->fill_query_vars($q);
 
+		// Parse meta query
+		$this->meta_query = new WP_Meta_Query();
+		$this->meta_query->parse_query_vars( $q );
+
 		// Set a flag if a pre_get_posts hook changed the query vars.
 		$hash = md5( serialize( $this->query_vars ) );
 		if ( $hash != $this->query_vars_hash ) {
@@ -2235,7 +2246,7 @@
 			}
 		}
 
-		if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
+		if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
 			$groupby = "{$wpdb->posts}.ID";
 		}
 
@@ -2468,18 +2479,8 @@
 			$where .= ')';
 		}
 
-		// Parse the meta query again if query vars have changed.
-		if ( $this->query_vars_changed ) {
-			$meta_query_hash = md5( serialize( $q['meta_query'] ) );
-			$_meta_query = $q['meta_query'];
-			unset( $q['meta_query'] );
-			_parse_meta_query( $q );
-			if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
-				$q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
-		}
-
-		if ( !empty( $q['meta_query'] ) ) {
-			$clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
+		if ( !empty( $this->meta_query->queries ) ) {
+			$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
 			$join .= $clauses['join'];
 			$where .= $clauses['where'];
 		}
Index: wp-includes/meta.php
===================================================================
--- wp-includes/meta.php	(revision 17693)
+++ wp-includes/meta.php	(working copy)
@@ -355,121 +355,190 @@
 /**
  * Given a meta query, generates SQL clauses to be appended to a main query
  *
- * @since 3.1.0
- * @access private
+ * @since 3.2.0
  *
- * @param array $meta_query List of metadata queries. A single query is an associative array:
- * - 'key' string The meta key
- * - 'value' string|array The meta value
- * - 'compare' (optional) string How to compare the key to the value.
- *		Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
- *		Default: '='
- * - 'type' string (optional) The type of the value.
- *		Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
- *		Default: 'CHAR'
+ * @see WP_Meta_Query
  *
+ * @param array (optional) $meta_query A meta query
  * @param string $type Type of meta
  * @param string $primary_table
  * @param string $primary_id_column
  * @param object $context (optional) The main query object
  * @return array( 'join' => $join_sql, 'where' => $where_sql )
  */
-function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
-	global $wpdb;
+function get_meta_sql( $meta_query = false, $type, $primary_table, $primary_id_column, $context = null ) {
+	$meta_query_obj = new WP_Meta_Query( $meta_query );
+	return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
+}
 
-	if ( ! $meta_table = _get_meta_table( $type ) )
-		return false;
+/**
+ * Container class for a multiple metadata query
+ *
+ * @since 3.2
+ */
+class WP_Meta_Query {
+	/**
+	* List of metadata queries. A single query is an associative array:
+	* - 'key' string The meta key
+	* - 'value' string|array The meta value
+	* - 'compare' (optional) string How to compare the key to the value.
+	*              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
+	*              Default: '='
+	* - 'type' string (optional) The type of the value.
+	*              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
+	*              Default: 'CHAR'
+	*
+	* @since 3.2
+	* @access public
+	* @var array
+	*/
+	public $queries = array();
 
-	$meta_id_column = esc_sql( $type . '_id' );
+	/**
+	 * The relation between the queries. Can be one of 'AND' or 'OR'.
+	 *
+	 * @since 3.2
+	 * @access public
+	 * @var string
+	 */	
+	public $relation;
 
-	$join = '';
-	$where = '';
-	$i = 0;
-	foreach ( $meta_query as $q ) {
-		$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
-		$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
-		$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
+	/**
+	 * Constructor
+	 *
+	 * @param array (optional) $meta_query A meta query
+	 */
+	function __construct( $meta_query = false ) {
+		if ( !$meta_query )
+			return;
 
-		if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
-			$meta_compare = '=';
+		if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
+			$this->relation = 'OR';
+		} else {
+			$this->relation = 'AND';
+		}
 
-		if ( 'NUMERIC' == $meta_type )
-			$meta_type = 'SIGNED';
-		elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
-			$meta_type = 'CHAR';
+		$this->queries = array();
 
-		if ( empty( $meta_key ) && empty( $meta_value ) )
-			continue;
+		foreach ( $meta_query as $key => $query ) {
+			if ( ! is_array( $query ) )
+				continue;
 
-		$alias = $i ? 'mt' . $i : $meta_table;
+			$this->queries[] = $query;
+		}
+	}
 
-		$join .= "\nINNER JOIN $meta_table";
-		$join .= $i ? " AS $alias" : '';
-		$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
+	/**
+	 * Constructs a meta query based on 'meta_*' query vars
+	 *
+	 * @since 3.2
+	 * @access public
+	 *
+	 * @param array $qv The query variables
+	 */
+	function parse_query_vars( $qv ) {
+		$meta_query = array();
 
-		$i++;
+		// Simple query needs to be first for orderby=meta_value to work correctly
+		foreach ( array( 'key', 'compare', 'type' ) as $key ) {
+			if ( !empty( $qv[ "meta_$key" ] ) )
+				$meta_query[0][ $key ] = $qv[ "meta_$key" ];
+		}
 
-		if ( !empty( $meta_key ) )
-			$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
+		// WP_Query sets 'meta_value' = '' by default
+		if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
+			$meta_query[0]['value'] = $qv[ 'meta_value' ];
 
-		if ( !isset( $q['value'] ) )
-			continue;
-		$meta_value = $q['value'];
+		if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
+			$meta_query = array_merge( $meta_query, $qv['meta_query'] );
+		}
 
-		if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
-			if ( ! is_array( $meta_value ) )
-				$meta_value = preg_split( '/[,\s]+/', $meta_value );
+		$this->__construct( $meta_query );
+	}
 
-			if ( empty( $meta_value ) )
+	/**
+	 * Generates SQL clauses to be appended to a main query.
+	 *
+	 * @since 3.2
+	 * @access public
+	 *
+	 * @param string $type Type of meta
+	 * @param string $primary_table
+	 * @param string $primary_id_column
+	 * @param object $context (optional) The main query object
+	 * @return array( 'join' => $join_sql, 'where' => $where_sql )
+	 */
+	function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
+		global $wpdb;
+
+		if ( ! $meta_table = _get_meta_table( $type ) )
+			return false;
+
+		$meta_id_column = esc_sql( $type . '_id' );
+
+		$join = '';
+		$where = array();
+		$i = 0;
+		foreach ( $this->queries as $k => $q ) {
+			$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
+			$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
+			$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
+
+			if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
+				$meta_compare = '=';
+
+			if ( 'NUMERIC' == $meta_type )
+				$meta_type = 'SIGNED';
+			elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
+				$meta_type = 'CHAR';
+
+			if ( empty( $meta_key ) && empty( $meta_value ) )
 				continue;
-		} else {
-			$meta_value = trim( $meta_value );
-		}
 
-		if ( 'IN' == substr( $meta_compare, -2) ) {
-			$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
-		} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
-			$meta_value = array_slice( $meta_value, 0, 2 );
-			$meta_compare_string = '%s AND %s';
-		} elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
-			$meta_value = '%' . like_escape( $meta_value ) . '%';
-			$meta_compare_string = '%s';
-		} else {
-			$meta_compare_string = '%s';
-		}
+			$alias = $i ? 'mt' . $i : $meta_table;
 
-		$where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
-	}
+			$join .= "\nINNER JOIN $meta_table";
+			$join .= $i ? " AS $alias" : '';
+			$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
 
-	return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );
-}
+			$i++;
 
-/**
- * Populates the $meta_query property
- *
- * @access private
- * @since 3.1.0
- *
- * @param array $qv The query variables
- */
-function _parse_meta_query( &$qv ) {
-	$meta_query = array();
+			if ( !empty( $meta_key ) )
+				$where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
 
-	// Simple query needs to be first for orderby=meta_value to work correctly
-	foreach ( array( 'key', 'compare', 'type' ) as $key ) {
-		if ( !empty( $qv[ "meta_$key" ] ) )
-			$meta_query[0][ $key ] = $qv[ "meta_$key" ];
-	}
+			if ( !isset( $q['value'] ) )
+				continue;
+			$meta_value = $q['value'];
 
-	// WP_Query sets 'meta_value' = '' by default
-	if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
-		$meta_query[0]['value'] = $qv[ 'meta_value' ];
+			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
+				if ( ! is_array( $meta_value ) )
+					$meta_value = preg_split( '/[,\s]+/', $meta_value );
 
-	if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
-		$meta_query = array_merge( $meta_query, $qv['meta_query'] );
+				if ( empty( $meta_value ) )
+					continue;
+			} else {
+				$meta_value = trim( $meta_value );
+			}
+
+			if ( 'IN' == substr( $meta_compare, -2) ) {
+				$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
+			} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
+				$meta_value = array_slice( $meta_value, 0, 2 );
+				$meta_compare_string = '%s AND %s';
+			} elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
+				$meta_value = '%' . like_escape( $meta_value ) . '%';
+				$meta_compare_string = '%s';
+			} else {
+				$meta_compare_string = '%s';
+			}
+
+			$where[$k] = ' (' . $where[$k] . $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );
+		}
+		$where = ' AND (' . implode( " {$this->relation} ", $where ) . ' )';
+
+		return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
 	}
 
-	$qv['meta_query'] = $meta_query;
 }
 
 /**
