diff --git src/wp-includes/class-wp-recursive-query.php src/wp-includes/class-wp-recursive-query.php
new file mode 100644
index 0000000..ca68ba8
--- /dev/null
+++ src/wp-includes/class-wp-recursive-query.php
@@ -0,0 +1,167 @@
+<?php
+
+/**
+ * Base class for creating query classes that generate SQL fragments for filtering results based on recursive query params.
+ *
+ *
+ *
+ * @since 4.1.0
+ */
+abstract class WP_Recursive_Query {
+
+	/**
+	 * Query arguments passed to the constructor.
+	 *
+	 * @since 4.1.0
+	 * @access public
+	 * @var array
+	 */
+	public $queries = array();
+
+	/**
+	 * A flat list of table aliases used in JOIN clauses.
+	 *
+	 * @since 4.1.0
+	 * @access protected
+	 * @var array
+	 */
+	protected $table_aliases = array();
+
+	/**
+	 * Generate SQL clauses to be appended to a main query.
+	 *
+	 * Extending classes should call this method from within a publicly
+	 * accessible get_sql() method, and manipulate the SQL as necessary.
+	 * For example, {@link WP_Tax_Query::get_sql()} is merely a wrapper for
+	 * get_sql_clauses(), while {@link WP_Date_Query::get_sql()} discards
+	 * the empty 'join' clauses are discarded, and passes the 'where'
+	 * clause through apply_filters().
+	 *
+	 * @since 4.1.0
+	 * @access protected
+	 *
+	 * @param  string $primary_table
+	 * @param  string $primary_id_column
+	 * @return array
+	 */
+	protected function get_sql_clauses() {
+		$sql = $this->get_sql_for_query( $this->queries );
+
+		if ( ! empty( $sql['where'] ) ) {
+			$sql['where'] = ' AND ' . "\n" . $sql['where'] . "\n";
+		}
+
+		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.
+	 *
+	 * Subclasses generally do not need to call this method. It is invoked
+	 * automatically from get_sql_clauses().
+	 *
+	 * @since 4.1.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
+	 */
+	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 .= "\t";
+		}
+
+		foreach ( $query as $key => $clause ) {
+			if ( 'relation' === $key ) {
+				$relation = $query['relation'];
+			} else {
+				// 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
+				} else {
+					$clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
+
+					$sql_chunks['where'][] = $clause_sql['where'];
+					$sql_chunks['join'][]  = $clause_sql['join'];
+
+				}
+			}
+		}
+
+		// Filter empties
+		$sql_chunks['join']  = array_filter( $sql_chunks['join'] );
+		$sql_chunks['where'] = array_filter( $sql_chunks['where'] );
+
+		if ( empty( $relation ) ) {
+			$relation = 'AND';
+		}
+
+		if ( ! empty( $sql_chunks['join'] ) ) {
+			$sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
+		}
+
+		if ( ! empty( $sql_chunks['where'] ) ) {
+			$sql['where'] = '( ' . "\n\t" . $indent . implode( ' ' . "\n\t" . $indent . $relation . ' ' . "\n\t" . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')' . "\n";
+		}
+
+		return $sql;
+	}
+
+	/**
+	 * Generate JOIN and WHERE clauses for a first-order clause.
+	 *
+	 * Must be overridden in a subclass.
+	 *
+	 * @since 4.1.0
+	 * @access protected
+	 *
+	 * @param  array $clause       Array of arguments belonging to the clause.
+	 * @param  array $parent_query Parent query to which the clause belongs.
+	 * @return array {
+	 *     @type array $join  Array of subclauses for the JOIN statement.
+	 *     @type array $where Array of subclauses for the WHERE statement.
+	 * }
+	 */
+	abstract protected function get_sql_for_clause( $clause, $parent_query );
+
+	/**
+	 * Determine whether a clause is first-order.
+	 *
+	 * Must be overridden in a subclass.
+	 *
+	 * @since 4.1.0
+	 * @access protected
+	 *
+	 * @param  array $q Clause to check.
+	 * @return bool
+	 */
+	abstract protected function is_first_order_clause( $query );
+}
diff --git src/wp-includes/meta.php src/wp-includes/meta.php
index e3c43cf..3b452fc 100644
--- src/wp-includes/meta.php
+++ src/wp-includes/meta.php
@@ -853,7 +853,7 @@ function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $
  *
  * @since 3.2.0
  */
-class WP_Meta_Query {
+class WP_Meta_Query extends WP_Recursive_Query {
 	/**
 	* List of metadata queries. A single query is an associative array:
 	* - 'key' string The meta key
@@ -882,9 +882,41 @@ class WP_Meta_Query {
 	public $relation;
 
 	/**
-	 * Constructor
+	 * Database table to query for the metadata.
 	 *
-	 * @param array $meta_query (optional) A meta query
+	 * @access public
+	 * @var string
+	 */
+	public $meta_table;
+
+	/**
+	 * Column in meta_table that represents the ID of the object the metadata belongs to.
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $meta_id_column;
+
+	/**
+	 * Database table that where the metadata's objects are stored (eg $wpdb->users).
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $primary_table;
+
+	/**
+	 * Column in primary_table that represents the ID of the object.
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $primary_id_column;
+
+	/**
+	 * Constructor.
+	 *
+	 * @param array $meta_query (optional) A meta query.
 	 */
 	public function __construct( $meta_query = false ) {
 		if ( !$meta_query )
@@ -896,14 +928,66 @@ class WP_Meta_Query {
 			$this->relation = 'AND';
 		}
 
-		$this->queries = array();
+		$this->queries = $this->sanitize_query( $meta_query );
+	}
+
+	/**
+	 * Ensure that the meta_query argument passed to the class constructor is well-formed.
+	 *
+	 * Eliminates empty items and ensures that a 'relation' is set.
+	 *
+	 * @param  array $queries
+	 * @return array
+	 */
+	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;
+
+			// First-order clause.
+			} else if ( $this->is_first_order_clause( $query ) ) {
+				$clean_queries[] = $query;
+
+			// Otherwise, it's a nested query.
+			} 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';
 
-		foreach ( $meta_query as $key => $query ) {
-			if ( ! is_array( $query ) )
-				continue;
+		// 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';
 
-			$this->queries[] = $query;
+		// Default to AND.
+		} else {
+			$clean_queries['relation'] = 'AND';
 		}
+
+		return $clean_queries;
+	}
+
+	protected function is_first_order_clause( $query ) {
+		return isset( $query['key'] ) || isset( $query['value'] );
 	}
 
 	/**
@@ -958,125 +1042,101 @@ class WP_Meta_Query {
 	}
 
 	/**
-	 * Generates SQL clauses to be appended to a main query.
+	 * Generate SQL JOIN and WHERE clauses for a first-order query clause.
 	 *
-	 * @since 3.2.0
-	 * @access public
+	 * "First-order" means that it's an array with a 'key' or 'value'.
 	 *
-	 * @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 )
+	 * @param  array $clause       Query clause.
+	 * @param  array $parent_query Parent query array.
+	 * @return array
 	 */
-	public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
+	public function get_sql_for_clause( $clause, $parent_query ) {
 		global $wpdb;
 
-		if ( ! $meta_table = _get_meta_table( $type ) )
-			return false;
-
-		$meta_id_column = sanitize_key( $type . '_id' );
-
-		$join = array();
-		$where = array();
+		$sql_chunks = array(
+			'where' => array(),
+			'join' => array(),
+		);
 
-		$key_only_queries = array();
-		$queries = array();
+		$i = count( $this->table_aliases );
+		$alias = $i ? 'mt' . $i : $this->meta_table;
 
-		// Split out the queries with empty arrays as value
-		foreach ( $this->queries as $k => $q ) {
-			if ( isset( $q['value'] ) && is_array( $q['value'] ) && empty( $q['value'] ) ) {
-				$key_only_queries[$k] = $q;
-				unset( $this->queries[$k] );
-			}
-		}
-
-		// Split out the meta_key only queries (we can only do this for OR)
-		if ( 'OR' == $this->relation ) {
-			foreach ( $this->queries as $k => $q ) {
-				if ( ( empty( $q['compare'] ) || 'NOT EXISTS' != $q['compare'] ) && ! array_key_exists( 'value', $q ) && ! empty( $q['key'] ) )
-					$key_only_queries[$k] = $q;
-				else
-					$queries[$k] = $q;
-			}
+		if ( isset( $clause['compare'] ) ) {
+			$meta_compare = strtoupper( $clause['compare'] );
 		} else {
-			$queries = $this->queries;
+			$meta_compare = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
 		}
 
-		// Specify all the meta_key only queries in one go
-		if ( $key_only_queries ) {
-			$join[]  = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
-
-			foreach ( $key_only_queries as $key => $q )
-				$where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
+		if ( ! in_array( $meta_compare, array(
+			'=', '!=', '>', '>=', '<', '<=',
+			'LIKE', 'NOT LIKE',
+			'IN', 'NOT IN',
+			'BETWEEN', 'NOT BETWEEN',
+			'EXISTS', 'NOT EXISTS',
+			'REGEXP', 'NOT REGEXP', 'RLIKE'
+		) ) ) {
+			$meta_compare = '=';
 		}
 
-		foreach ( $queries as $k => $q ) {
-			$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
-			$meta_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' );
-
-			if ( array_key_exists( 'value', $q ) && is_null( $q['value'] ) )
-				$q['value'] = '';
-
-			$meta_value = isset( $q['value'] ) ? $q['value'] : null;
-
-			if ( isset( $q['compare'] ) )
-				$meta_compare = strtoupper( $q['compare'] );
-			else
-				$meta_compare = is_array( $meta_value ) ? 'IN' : '=';
-
-			if ( ! in_array( $meta_compare, array(
-				'=', '!=', '>', '>=', '<', '<=',
-				'LIKE', 'NOT LIKE',
-				'IN', 'NOT IN',
-				'BETWEEN', 'NOT BETWEEN',
-				'NOT EXISTS',
-				'REGEXP', 'NOT REGEXP', 'RLIKE'
-			) ) )
-				$meta_compare = '=';
-
-			$i = count( $join );
-			$alias = $i ? 'mt' . $i : $meta_table;
-
-			if ( 'NOT EXISTS' == $meta_compare ) {
-				$join[$i]  = "LEFT JOIN $meta_table";
-				$join[$i] .= $i ? " AS $alias" : '';
-				$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";
-
-				$where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';
-
-				continue;
+		// There are a number of different query structures that get
+		// built in different ways.
+		// 1. Key-only clauses - (a) clauses without a 'value' key that
+		//    appear in the context of an OR relation and do not use
+		//    'NOT EXISTS' as the 'compare', or (b) clauses with an
+		//    empty array for 'value'.
+		if ( ! empty( $clause['key'] ) && (
+			( ! array_key_exists( 'value', $clause ) && 'NOT EXISTS' !== $meta_compare && 'OR' === $parent_query['relation'] ) ||
+			( isset( $clause['value'] ) && is_array( $clause['value'] ) && empty( $clause['value'] ) )
+		) ) {
+
+			$alias = $this->meta_table;
+			$sql_chunks['join'][] = " INNER JOIN $this->meta_table ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column)";
+			$sql_chunks['where'][] = $wpdb->prepare( "$this->meta_table.meta_key = %s", trim( $clause['key'] ) );
+
+		// 2. NOT EXISTS.
+		} else if ( 'NOT EXISTS' === $meta_compare ) {
+			$join  = " LEFT JOIN $this->meta_table";
+			$join .= $i ? " AS $alias" : '';
+			$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
+			$sql_chunks['join'][] = $join;
+
+			$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
+
+		// 3. EXISTS and other key-only queries.
+		} else if ( 'EXISTS' === $meta_compare || ( ! empty( $clause['key'] ) && ! array_key_exists( 'value', $clause ) ) ) {
+			$join  = " INNER JOIN $this->meta_table";
+			$join .= $i ? " AS $alias" : '';
+			$join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";
+			$sql_chunks['join'][] = $join;
+
+			$sql_chunks['where'][] = $wpdb->prepare( $alias . '.meta_key = %s', trim( $clause['key'] ) );
+
+		// 4. Clauses that have a value.
+		} else if ( array_key_exists( 'value', $clause ) ) {
+			$join  = " INNER JOIN $this->meta_table";
+			$join .= $i ? " AS $alias" : '';
+			$join .= " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column)";
+			$sql_chunks['join'][] = $join;
+
+			if ( ! empty( $clause['key'] ) ) {
+				$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
 			}
 
-			$join[$i]  = "INNER JOIN $meta_table";
-			$join[$i] .= $i ? " AS $alias" : '';
-			$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
-
-			$where[$k] = '';
-			if ( !empty( $meta_key ) )
-				$where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
+			$meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
 
-			if ( is_null( $meta_value ) ) {
-				if ( empty( $where[$k] ) )
-					unset( $join[$i] );
-				continue;
-			}
+			$meta_value = isset( $clause['value'] ) ? $clause['value'] : '';
 
 			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
-				if ( ! is_array( $meta_value ) )
+				if ( ! is_array( $meta_value ) ) {
 					$meta_value = preg_split( '/[,\s]+/', $meta_value );
-
-				if ( empty( $meta_value ) ) {
-					unset( $join[$i] );
-					continue;
 				}
 			} else {
 				$meta_value = trim( $meta_value );
 			}
 
-			if ( 'IN' == substr( $meta_compare, -2) ) {
+			if ( 'IN' == substr( $meta_compare, -2 ) ) {
 				$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
-			} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
+			} elseif ( 'BETWEEN' == substr( $meta_compare, -7 ) ) {
 				$meta_value = array_slice( $meta_value, 0, 2 );
 				$meta_compare_string = '%s AND %s';
 			} elseif ( 'LIKE' == $meta_compare || 'NOT LIKE' == $meta_compare ) {
@@ -1086,22 +1146,50 @@ class WP_Meta_Query {
 				$meta_compare_string = '%s';
 			}
 
-			if ( ! empty( $where[$k] ) )
-				$where[$k] .= ' AND ';
+			$sql_chunks['where'][] = $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
+		}
 
-			$where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );
+		// 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'] ) . ' )' );
 		}
 
-		$where = array_filter( $where );
+		$this->table_aliases[] = $alias;
 
-		if ( empty( $where ) )
-			$where = '';
-		else
-			$where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )';
+		return $sql_chunks;
+	}
+
+	/**
+	 * Generates SQL clauses to be appended to a main query.
+	 *
+	 * @since 3.2.0
+	 * @access public
+	 *
+	 * @param  string $type              Type of meta, eg 'user', 'post'.
+	 * @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
+	 * @param  object $context           Optional. The main query object.
+	 * @return 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.
+	 * }
+	 */
+	public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
+		global $wpdb;
+
+		if ( ! $meta_table = _get_meta_table( $type ) ) {
+			return false;
+		}
+
+		$this->meta_table     = $meta_table;
+		$this->meta_id_column = sanitize_key( $type . '_id' );
+
+		$this->primary_table     = $primary_table;
+		$this->primary_id_column = $primary_id_column;
 
-		$join = implode( "\n", $join );
-		if ( ! empty( $join ) )
-			$join = ' ' . $join;
+		$sql = $this->get_sql_clauses();
 
 		/**
 		 * Filter the meta query's generated SQL.
@@ -1119,7 +1207,7 @@ class WP_Meta_Query {
 		 *     @type object $context           The main query object.
 		 * }
 		 */
-		return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
+		return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
 	}
 }
 
diff --git src/wp-settings.php src/wp-settings.php
index 9795971..5dca996 100644
--- src/wp-settings.php
+++ src/wp-settings.php
@@ -111,6 +111,7 @@ wp_not_installed();
 // Load most of WordPress.
 require( ABSPATH . WPINC . '/class-wp-walker.php' );
 require( ABSPATH . WPINC . '/class-wp-ajax-response.php' );
+require( ABSPATH . WPINC . '/class-wp-recursive-query.php' );
 require( ABSPATH . WPINC . '/formatting.php' );
 require( ABSPATH . WPINC . '/capabilities.php' );
 require( ABSPATH . WPINC . '/query.php' );
diff --git tests/phpunit/tests/meta/query.php tests/phpunit/tests/meta/query.php
index fb2a1cc..390ccc9 100644
--- tests/phpunit/tests/meta/query.php
+++ tests/phpunit/tests/meta/query.php
@@ -40,7 +40,7 @@ class Tests_Meta_Query extends WP_UnitTestCase {
 			array(),
 		) );
 
-		$this->assertSame( array( array() ), $query->queries );
+		$this->assertSame( array(), $query->queries );
 	}
 
 	/**
@@ -153,19 +153,44 @@ class Tests_Meta_Query extends WP_UnitTestCase {
 		$query = new WP_Meta_Query();
 
 		// just meta_value
-		$query->parse_query_vars( array( 'meta_key' => 'abc' ) );
-
-		$this->assertEquals( array( array( 'key' => 'abc' ) ), $query->queries );
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'key' => 'abc',
+			),
+		);
+		$query->parse_query_vars( array(
+			'meta_key' => 'abc',
+		) );
+		$this->assertEquals( $expected, $query->queries );
 
 		// meta_key & meta_value
-		$query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_value' => 'def' ) );
-
-		$this->assertEquals( array( array( 'key' => 'abc', 'value' => 'def' ) ), $query->queries );
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'key' => 'abc',
+				'value' => 'def',
+			),
+		);
+		$query->parse_query_vars( array(
+			'meta_key' => 'abc',
+			'meta_value' => 'def',
+		) );
+		$this->assertEquals( $expected, $query->queries );
 
 		// meta_compare
-		$query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_compare' => '=>' ) );
-
-		$this->assertEquals( array( array( 'key' => 'abc', 'compare' => '=>' ) ), $query->queries );
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'key' => 'abc',
+				'compare' => '=>',
+			),
+		);
+		$query->parse_query_vars( array(
+			'meta_key' => 'abc',
+			'meta_compare' => '=>',
+		) );
+		$this->assertEquals( $expected, $query->queries );
 	}
 
 	/**
@@ -202,6 +227,229 @@ class Tests_Meta_Query extends WP_UnitTestCase {
 		$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
 	}
 
+	public function test_sanitize_query_single_query() {
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
+	public function test_sanitize_query_multiple_first_order_queries_relation_default() {
+		$expected = array(
+			'relation' => 'AND',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
+	public function test_sanitize_query_multiple_first_order_queries_relation_or() {
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			'relation' => 'OR',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
+	public function test_sanitize_query_multiple_first_order_queries_relation_or_lowercase() {
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			'relation' => 'or',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
+	public function test_sanitize_query_multiple_first_order_queries_invalid_relation() {
+		$expected = array(
+			'relation' => 'AND',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			'relation' => 'FOO',
+			array(
+				'key' => 'foo',
+				'value' => 'bar',
+			),
+			array(
+				'key' => 'foo2',
+				'value' => 'bar2',
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
+	public function test_sanitize_query_single_query_which_is_a_nested_query() {
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'relation' => 'AND',
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+				array(
+					'key' => 'foo2',
+					'value' => 'bar2',
+				),
+			)
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+				array(
+					'key' => 'foo2',
+					'value' => 'bar2',
+				),
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
+	public function test_sanitize_query_multiple_nested_queries() {
+		$expected = array(
+			'relation' => 'OR',
+			array(
+				'relation' => 'AND',
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+				array(
+					'key' => 'foo2',
+					'value' => 'bar2',
+				),
+			),
+			array(
+				'relation' => 'AND',
+				array(
+					'key' => 'foo3',
+					'value' => 'bar3',
+				),
+				array(
+					'key' => 'foo4',
+					'value' => 'bar4',
+				),
+			),
+		);
+
+		$q = new WP_Meta_Query();
+		$found = $q->sanitize_query( array(
+			'relation' => 'OR',
+			array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+				array(
+					'key' => 'foo2',
+					'value' => 'bar2',
+				),
+			),
+			array(
+				array(
+					'key' => 'foo3',
+					'value' => 'bar3',
+				),
+				array(
+					'key' => 'foo4',
+					'value' => 'bar4',
+				),
+			),
+		) );
+
+		$this->assertEquals( $expected, $found );
+	}
+
 	/**
 	 * Invalid $type will fail to get a table from _get_meta_table()
 	 */
@@ -229,7 +477,7 @@ class Tests_Meta_Query extends WP_UnitTestCase {
 		$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
 
 		// We should have 2 joins - one for my_first_key and one for my_second_key
-		$this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
+		$this->assertEquals( 2, substr_count( $sql['join'], 'JOIN' ) );
 
 		// The WHERE should check my_third_key against an unaliased table
 		$this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
@@ -247,7 +495,7 @@ class Tests_Meta_Query extends WP_UnitTestCase {
 		) );
 		$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
 
-		$this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
+		$this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) );
 	}
 
 	/**
@@ -558,7 +806,9 @@ class Tests_Meta_Query extends WP_UnitTestCase {
 		) );
 
 		$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
-		$this->assertContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
+
+		// Use regex because we don't care about the whitespace before OR.
+		$this->assertRegExp( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] );
 		$this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
 	}
 }
diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
index 45b1abc..03edcf9 100644
--- tests/phpunit/tests/post/query.php
+++ tests/phpunit/tests/post/query.php
@@ -772,6 +772,89 @@ class Tests_Post_Query extends WP_UnitTestCase {
 		$this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts );
 	}
 
+	public function test_meta_query_nested() {
+		$p1 = $this->factory->post->create();
+		$p2 = $this->factory->post->create();
+		$p3 = $this->factory->post->create();
+
+		add_post_meta( $p1, 'foo', 'bar' );
+		add_post_meta( $p2, 'foo2', 'bar' );
+		add_post_meta( $p3, 'foo2', 'bar' );
+		add_post_meta( $p3, 'foo3', 'bar' );
+
+		$query = new WP_Query( array(
+			'update_post_meta_cache' => false,
+			'update_term_meta_cache' => false,
+			'fields' => 'ids',
+			'meta_query' => array(
+				'relation' => 'OR',
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+				array(
+					'relation' => 'AND',
+					array(
+						'key' => 'foo2',
+						'value' => 'bar',
+					),
+					array(
+						'key' => 'foo3',
+						'value' => 'bar',
+					),
+				),
+			),
+		) );
+
+		$expected = array( $p1, $p3 );
+		$this->assertEqualSets( $expected, $query->posts );
+	}
+
+	public function test_meta_query_nested_two_levels_deep() {
+		$p1 = $this->factory->post->create();
+		$p2 = $this->factory->post->create();
+		$p3 = $this->factory->post->create();
+
+		add_post_meta( $p1, 'foo', 'bar' );
+		add_post_meta( $p3, 'foo2', 'bar' );
+		add_post_meta( $p3, 'foo3', 'bar' );
+		add_post_meta( $p3, 'foo4', 'bar' );
+
+		$query = new WP_Query( array(
+			'update_post_meta_cache' => false,
+			'update_term_meta_cache' => false,
+			'fields' => 'ids',
+			'meta_query' => array(
+				'relation' => 'OR',
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+				array(
+					'relation' => 'OR',
+					array(
+						'key' => 'foo2',
+						'value' => 'bar',
+					),
+					array(
+						'relation' => 'AND',
+						array(
+							'key' => 'foo3',
+							'value' => 'bar',
+						),
+						array(
+							'key' => 'foo4',
+							'value' => 'bar',
+						),
+					),
+				),
+			),
+		) );
+
+		$expected = array( $p1, $p3 );
+		$this->assertEqualSets( $expected, $query->posts );
+	}
+
 	/**
 	 * @ticket 20604
 	 */
