Index: /trunk/src/wp-includes/class-wp-query.php
===================================================================
--- /trunk/src/wp-includes/class-wp-query.php	(revision 59765)
+++ /trunk/src/wp-includes/class-wp-query.php	(revision 59766)
@@ -474,4 +474,14 @@
 
 	private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
+
+	/**
+	 * The cache key generated by the query.
+	 *
+	 * The cache key is generated by the method ::generate_cache_key() after the
+	 * query has been normalized.
+	 *
+	 * @var string
+	 */
+	private $query_cache_key = '';
 
 	/**
@@ -1102,5 +1112,6 @@
 		if ( ! empty( $qv['post_type'] ) ) {
 			if ( is_array( $qv['post_type'] ) ) {
-				$qv['post_type'] = array_map( 'sanitize_key', $qv['post_type'] );
+				$qv['post_type'] = array_map( 'sanitize_key', array_unique( $qv['post_type'] ) );
+				sort( $qv['post_type'] );
 			} else {
 				$qv['post_type'] = sanitize_key( $qv['post_type'] );
@@ -1110,5 +1121,6 @@
 		if ( ! empty( $qv['post_status'] ) ) {
 			if ( is_array( $qv['post_status'] ) ) {
-				$qv['post_status'] = array_map( 'sanitize_key', $qv['post_status'] );
+				$qv['post_status'] = array_map( 'sanitize_key', array_unique( $qv['post_status'] ) );
+				sort( $qv['post_status'] );
 			} else {
 				$qv['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $qv['post_status'] );
@@ -1183,7 +1195,10 @@
 				$term = $q[ $t->query_var ];
 
-				if ( is_array( $term ) ) {
-					$term = implode( ',', $term );
-				}
+				if ( ! is_array( $term ) ) {
+					$term = explode( ',', $term );
+					$term = array_map( 'trim', $term );
+				}
+				sort( $term );
+				$term = implode( ',', $term );
 
 				if ( str_contains( $term, '+' ) ) {
@@ -1221,5 +1236,6 @@
 			$cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
 			$cat_array = array_map( 'intval', $cat_array );
-			$q['cat']  = implode( ',', $cat_array );
+			sort( $cat_array );
+			$q['cat'] = implode( ',', $cat_array );
 
 			foreach ( $cat_array as $cat ) {
@@ -1263,5 +1279,6 @@
 		if ( ! empty( $q['category__in'] ) ) {
 			$q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
-			$tax_query[]       = array(
+			sort( $q['category__in'] );
+			$tax_query[] = array(
 				'taxonomy'         => 'category',
 				'terms'            => $q['category__in'],
@@ -1273,4 +1290,5 @@
 		if ( ! empty( $q['category__not_in'] ) ) {
 			$q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
+			sort( $q['category__not_in'] );
 			$tax_query[]           = array(
 				'taxonomy'         => 'category',
@@ -1283,5 +1301,6 @@
 		if ( ! empty( $q['category__and'] ) ) {
 			$q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
-			$tax_query[]        = array(
+			sort( $q['category__and'] );
+			$tax_query[] = array(
 				'taxonomy'         => 'category',
 				'terms'            => $q['category__and'],
@@ -1301,8 +1320,10 @@
 		if ( '' !== $q['tag'] && ! $this->is_singular && $this->query_vars_changed ) {
 			if ( str_contains( $q['tag'], ',' ) ) {
+				// @todo Handle normalizing `tag` query string.
 				$tags = preg_split( '/[,\r\n\t ]+/', $q['tag'] );
 				foreach ( (array) $tags as $tag ) {
 					$tag                 = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' );
 					$q['tag_slug__in'][] = $tag;
+					sort( $q['tag_slug__in'] );
 				}
 			} elseif ( preg_match( '/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) {
@@ -1315,4 +1336,5 @@
 				$q['tag']            = sanitize_term_field( 'slug', $q['tag'], 0, 'post_tag', 'db' );
 				$q['tag_slug__in'][] = $q['tag'];
+				sort( $q['tag_slug__in'] );
 			}
 		}
@@ -1328,5 +1350,6 @@
 		if ( ! empty( $q['tag__in'] ) ) {
 			$q['tag__in'] = array_map( 'absint', array_unique( (array) $q['tag__in'] ) );
-			$tax_query[]  = array(
+			sort( $q['tag__in'] );
+			$tax_query[] = array(
 				'taxonomy' => 'post_tag',
 				'terms'    => $q['tag__in'],
@@ -1336,4 +1359,5 @@
 		if ( ! empty( $q['tag__not_in'] ) ) {
 			$q['tag__not_in'] = array_map( 'absint', array_unique( (array) $q['tag__not_in'] ) );
+			sort( $q['tag__not_in'] );
 			$tax_query[]      = array(
 				'taxonomy' => 'post_tag',
@@ -1345,5 +1369,6 @@
 		if ( ! empty( $q['tag__and'] ) ) {
 			$q['tag__and'] = array_map( 'absint', array_unique( (array) $q['tag__and'] ) );
-			$tax_query[]   = array(
+			sort( $q['tag__and'] );
+			$tax_query[] = array(
 				'taxonomy' => 'post_tag',
 				'terms'    => $q['tag__and'],
@@ -1354,5 +1379,6 @@
 		if ( ! empty( $q['tag_slug__in'] ) ) {
 			$q['tag_slug__in'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
-			$tax_query[]       = array(
+			sort( $q['tag_slug__in'] );
+			$tax_query[] = array(
 				'taxonomy' => 'post_tag',
 				'terms'    => $q['tag_slug__in'],
@@ -1363,5 +1389,6 @@
 		if ( ! empty( $q['tag_slug__and'] ) ) {
 			$q['tag_slug__and'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
-			$tax_query[]        = array(
+			sort( $q['tag_slug__and'] );
+			$tax_query[] = array(
 				'taxonomy' => 'post_tag',
 				'terms'    => $q['tag_slug__and'],
@@ -2187,6 +2214,9 @@
 		} elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
 			$q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
-			$post_name__in      = "'" . implode( "','", $q['post_name__in'] ) . "'";
-			$where             .= " AND {$wpdb->posts}.post_name IN ($post_name__in)";
+			// Duplicate array before sorting to allow for the orderby clause.
+			$post_name__in_for_where = array_unique( $q['post_name__in'] );
+			sort( $post_name__in_for_where );
+			$post_name__in = "'" . implode( "','", $post_name__in_for_where ) . "'";
+			$where        .= " AND {$wpdb->posts}.post_name IN ($post_name__in)";
 		}
 
@@ -2200,7 +2230,12 @@
 			$where .= " AND {$wpdb->posts}.ID = " . $q['p'];
 		} elseif ( $q['post__in'] ) {
-			$post__in = implode( ',', array_map( 'absint', $q['post__in'] ) );
+			// Duplicate array before sorting to allow for the orderby clause.
+			$post__in_for_where = $q['post__in'];
+			$post__in_for_where = array_unique( array_map( 'absint', $post__in_for_where ) );
+			sort( $post__in_for_where );
+			$post__in = implode( ',', array_map( 'absint', $post__in_for_where ) );
 			$where   .= " AND {$wpdb->posts}.ID IN ($post__in)";
 		} elseif ( $q['post__not_in'] ) {
+			sort( $q['post__not_in'] );
 			$post__not_in = implode( ',', array_map( 'absint', $q['post__not_in'] ) );
 			$where       .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
@@ -2210,7 +2245,12 @@
 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] );
 		} elseif ( $q['post_parent__in'] ) {
-			$post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
+			// Duplicate array before sorting to allow for the orderby clause.
+			$post_parent__in_for_where = $q['post_parent__in'];
+			$post_parent__in_for_where = array_unique( array_map( 'absint', $post_parent__in_for_where ) );
+			sort( $post_parent__in_for_where );
+			$post_parent__in = implode( ',', array_map( 'absint', $post_parent__in_for_where ) );
 			$where          .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
 		} elseif ( $q['post_parent__not_in'] ) {
+			sort( $q['post_parent__not_in'] );
 			$post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
 			$where              .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
@@ -2342,4 +2382,5 @@
 			$q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
 			$authors     = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
+			sort( $authors );
 			foreach ( $authors as $author ) {
 				$key         = $author > 0 ? 'author__in' : 'author__not_in';
@@ -2350,7 +2391,15 @@
 
 		if ( ! empty( $q['author__not_in'] ) ) {
-			$author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
+			if ( is_array( $q['author__not_in'] ) ) {
+				$q['author__not_in'] = array_unique( array_map( 'absint', $q['author__not_in'] ) );
+				sort( $q['author__not_in'] );
+			}
+			$author__not_in = implode( ',', (array) $q['author__not_in'] );
 			$where         .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
 		} elseif ( ! empty( $q['author__in'] ) ) {
+			if ( is_array( $q['author__in'] ) ) {
+				$q['author__in'] = array_unique( array_map( 'absint', $q['author__in'] ) );
+				sort( $q['author__in'] );
+			}
 			$author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
 			$where     .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
@@ -2589,4 +2638,5 @@
 				$q_status = explode( ',', $q_status );
 			}
+			sort( $q_status );
 			$r_status = array();
 			$p_status = array();
@@ -4903,4 +4953,31 @@
 		sort( $args['post_type'] );
 
+		/*
+		 * Sort arrays that can be used for ordering prior to cache key generation.
+		 *
+		 * These arrays are sorted in the query generator for the purposes of the
+		 * WHERE clause but the arguments are not modified as they can be used for
+		 * the orderby clase.
+		 *
+		 * Their use in the orderby clause will generate a different SQL query so
+		 * they can be sorted for the cache key generation.
+		 */
+		$sortable_arrays_with_int_values = array(
+			'post__in',
+			'post_parent__in',
+		);
+		foreach ( $sortable_arrays_with_int_values as $key ) {
+			if ( isset( $args[ $key ] ) && is_array( $args[ $key ] ) ) {
+				$args[ $key ] = array_unique( array_map( 'absint', $args[ $key ] ) );
+				sort( $args[ $key ] );
+			}
+		}
+
+		// Sort and unique the 'post_name__in' for cache key generation.
+		if ( isset( $args['post_name__in'] ) && is_array( $args['post_name__in'] ) ) {
+			$args['post_name__in'] = array_unique( $args['post_name__in'] );
+			sort( $args['post_name__in'] );
+		}
+
 		if ( isset( $args['post_status'] ) ) {
 			$args['post_status'] = (array) $args['post_status'];
@@ -4943,5 +5020,6 @@
 		}
 
-		return "wp_query:$key:$last_changed";
+		$this->query_cache_key = "wp_query:$key:$last_changed";
+		return $this->query_cache_key;
 	}
 
Index: /trunk/tests/phpunit/tests/query/cacheResults.php
===================================================================
--- /trunk/tests/phpunit/tests/query/cacheResults.php	(revision 59765)
+++ /trunk/tests/phpunit/tests/query/cacheResults.php	(revision 59766)
@@ -202,12 +202,21 @@
 
 	/**
-	 * @ticket 59442
+	 * @ticket 59516
 	 *
 	 * @covers WP_Query::generate_cache_key
-	 *
-	 * @dataProvider data_query_cache_duplicate
-	 */
-	public function test_generate_cache_key_normalize( $query_vars1, $query_vars2 ) {
+	 */
+	public function test_post_in_order_by_clauses_are_not_normalized() {
 		global $wpdb;
+
+		$post_ids = self::$posts;
+
+		$query_vars1 = array(
+			'post__in' => $post_ids,
+			'orderby'  => 'post__in',
+		);
+		$query_vars2 = array(
+			'post__in' => array_reverse( $post_ids ),
+			'orderby'  => 'post__in',
+		);
 
 		$fields   = "{$wpdb->posts}.ID";
@@ -218,13 +227,176 @@
 		$request2 = str_replace( $fields, "{$wpdb->posts}.*", $query2->request );
 
-		$reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
-		$reflection->setAccessible( true );
+		$reflection_q1 = new ReflectionProperty( $query1, 'query_cache_key' );
+		$reflection_q1->setAccessible( true );
+
+		$reflection_q2 = new ReflectionProperty( $query2, 'query_cache_key' );
+		$reflection_q2->setAccessible( true );
+
+		$this->assertNotSame( $request1, $request2, 'Queries should not match' );
+
+		$cache_key_1 = $reflection_q1->getValue( $query1 );
+		$cache_key_2 = $reflection_q2->getValue( $query2 );
+
+		$this->assertNotSame( $cache_key_1, $cache_key_2, 'Cache key should differ.' );
+		$this->assertNotEmpty( $cache_key_1, 'Cache key for query one should not be empty.' );
+		$this->assertNotEmpty( $cache_key_2, 'Cache key for query two should not be empty.' );
+
+		// Test the posts are returned different orders.
+		$this->assertNotSame( wp_list_pluck( $query1->posts, 'ID' ), wp_list_pluck( $query2->posts, 'ID' ), 'Query one posts should not match the order of query two posts.' );
+		// Test the posts are the same sets.
+		$this->assertSameSets( wp_list_pluck( $query1->posts, 'ID' ), wp_list_pluck( $query2->posts, 'ID' ), 'Query one posts should match the set of query two posts.' );
+	}
+
+	/**
+	 * @ticket 59516
+	 *
+	 * @covers WP_Query::generate_cache_key
+	 */
+	public function test_post_parent_in_order_by_clauses_are_not_normalized() {
+		global $wpdb;
+
+		$parent_pages = self::$pages;
+		$post_names   = array( 'doctor-dillamond', 'elphaba', 'fiyero', 'glinda', 'the-wizard-of-oz' );
+		$child_pages  = array();
+		foreach ( $parent_pages as $key => $parent_page ) {
+			$child_pages[] = self::factory()->post->create(
+				array(
+					'post_parent' => $parent_page,
+					'post_type'   => 'page',
+					'post_name'   => $post_names[ $key ],
+				)
+			);
+		}
+
+		$query_vars1 = array(
+			'post_parent__in' => $parent_pages,
+			'post_type'       => 'page',
+			'orderby'         => 'post_parent__in',
+		);
+
+		$query_vars2 = array(
+			'post_parent__in' => array_reverse( $parent_pages ),
+			'post_type'       => 'page',
+			'orderby'         => 'post_parent__in',
+		);
+
+		$fields   = "{$wpdb->posts}.ID";
+		$query1   = new WP_Query( $query_vars1 );
+		$request1 = str_replace( $fields, "{$wpdb->posts}.*", $query1->request );
+
+		$query2   = new WP_Query( $query_vars2 );
+		$request2 = str_replace( $fields, "{$wpdb->posts}.*", $query2->request );
+
+		$reflection_q1 = new ReflectionProperty( $query1, 'query_cache_key' );
+		$reflection_q1->setAccessible( true );
+
+		$reflection_q2 = new ReflectionProperty( $query2, 'query_cache_key' );
+		$reflection_q2->setAccessible( true );
+
+		$this->assertNotSame( $request1, $request2, 'Queries should not match' );
+
+		$cache_key_1 = $reflection_q1->getValue( $query1 );
+		$cache_key_2 = $reflection_q2->getValue( $query2 );
+
+		$this->assertNotSame( $cache_key_1, $cache_key_2, 'Cache key should differ.' );
+		$this->assertNotEmpty( $cache_key_1, 'Cache key for query one should not be empty.' );
+		$this->assertNotEmpty( $cache_key_2, 'Cache key for query two should not be empty.' );
+
+		// Test the posts are returned in the correct order.
+		$this->assertSame( array( 'doctor-dillamond', 'elphaba', 'fiyero', 'glinda', 'the-wizard-of-oz' ), wp_list_pluck( $query1->posts, 'post_name' ), 'Query one posts should be in alphabetical order' );
+		$this->assertSame( array( 'the-wizard-of-oz', 'glinda', 'fiyero', 'elphaba', 'doctor-dillamond' ), wp_list_pluck( $query2->posts, 'post_name' ), 'Query two posts should be in reverse alphabetical order.' );
+		// Test the posts are the same sets.
+		$this->assertSameSets( wp_list_pluck( $query1->posts, 'ID' ), wp_list_pluck( $query2->posts, 'ID' ), 'Query one posts should match the set of query two posts.' );
+	}
+
+	/**
+	 * @ticket 59516
+	 *
+	 * @covers WP_Query::generate_cache_key
+	 */
+	public function test_post_name_in_order_by_clauses_are_not_normalized() {
+		global $wpdb;
+		$post_names = array( 'doctor-dillamond', 'elphaba', 'glinda', 'the-wizard-of-oz' );
+		$posts      = array();
+
+		foreach ( $post_names as $post_name ) {
+			$posts[] = self::factory()->post->create(
+				array(
+					'post_name' => $post_name,
+				)
+			);
+		}
+
+		$query_vars1 = array(
+			'post_name__in' => $post_names,
+			'orderby'       => 'post_name__in',
+		);
+
+		$query_vars2 = array(
+			'post_name__in' => array_reverse( $post_names ),
+			'orderby'       => 'post_name__in',
+		);
+
+		$fields   = "{$wpdb->posts}.ID";
+		$query1   = new WP_Query( $query_vars1 );
+		$request1 = str_replace( $fields, "{$wpdb->posts}.*", $query1->request );
+
+		$query2   = new WP_Query( $query_vars2 );
+		$request2 = str_replace( $fields, "{$wpdb->posts}.*", $query2->request );
+
+		$reflection_q1 = new ReflectionProperty( $query1, 'query_cache_key' );
+		$reflection_q1->setAccessible( true );
+
+		$reflection_q2 = new ReflectionProperty( $query2, 'query_cache_key' );
+		$reflection_q2->setAccessible( true );
+
+		$this->assertNotSame( $request1, $request2, 'Queries should not match' );
+
+		$cache_key_1 = $reflection_q1->getValue( $query1 );
+		$cache_key_2 = $reflection_q2->getValue( $query2 );
+
+		$this->assertNotSame( $cache_key_1, $cache_key_2, 'Cache key should differ.' );
+		$this->assertNotEmpty( $cache_key_1, 'Cache key for query one should not be empty.' );
+		$this->assertNotEmpty( $cache_key_2, 'Cache key for query two should not be empty.' );
+
+		// Test the posts are returned in the correct order.
+		$this->assertSame( array( 'doctor-dillamond', 'elphaba', 'glinda', 'the-wizard-of-oz' ), wp_list_pluck( $query1->posts, 'post_name' ), 'Query one posts should be in alphabetical order' );
+		$this->assertSame( array( 'the-wizard-of-oz', 'glinda', 'elphaba', 'doctor-dillamond' ), wp_list_pluck( $query2->posts, 'post_name' ), 'Query two posts should be in reverse alphabetical order.' );
+		// Test the posts are the same sets.
+		$this->assertSameSets( wp_list_pluck( $query1->posts, 'ID' ), wp_list_pluck( $query2->posts, 'ID' ), 'Query one posts should match the set of query two posts.' );
+	}
+
+	/**
+	 * @ticket 59442
+	 * @ticket 59516
+	 *
+	 * @covers WP_Query::generate_cache_key
+	 *
+	 * @dataProvider data_query_cache_duplicate
+	 */
+	public function test_generate_cache_key_normalize( $query_vars1, $query_vars2 ) {
+		global $wpdb;
+
+		$fields   = "{$wpdb->posts}.ID";
+		$query1   = new WP_Query( $query_vars1 );
+		$request1 = str_replace( $fields, "{$wpdb->posts}.*", $query1->request );
+
+		$query2   = new WP_Query( $query_vars2 );
+		$request2 = str_replace( $fields, "{$wpdb->posts}.*", $query2->request );
+
+		$reflection_q1 = new ReflectionProperty( $query1, 'query_cache_key' );
+		$reflection_q1->setAccessible( true );
+
+		$reflection_q2 = new ReflectionProperty( $query2, 'query_cache_key' );
+		$reflection_q2->setAccessible( true );
 
 		$this->assertSame( $request1, $request2, 'Queries should match' );
 
-		$cache_key_1 = $reflection->invoke( $query1, $query_vars1, $request1 );
-		$cache_key_2 = $reflection->invoke( $query1, $query_vars2, $request2 );
-
-		$this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs the same paramters.' );
+		$cache_key_1 = $reflection_q1->getValue( $query1 );
+		$cache_key_2 = $reflection_q2->getValue( $query2 );
+
+		$this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs the same effective parameters.' );
+		$this->assertNotEmpty( $cache_key_1, 'Cache key for query one should not be empty.' );
+		$this->assertNotEmpty( $cache_key_2, 'Cache key for query two should not be empty.' );
 	}
 
@@ -281,17 +453,17 @@
 	public function data_query_cache_duplicate() {
 		return array(
-			'post type empty'           => array(
+			'post type empty'                              => array(
 				'query_vars1' => array( 'post_type' => '' ),
 				'query_vars2' => array( 'post_type' => 'post' ),
 			),
-			'post type array'           => array(
+			'post type array'                              => array(
 				'query_vars1' => array( 'post_type' => array( 'page' ) ),
 				'query_vars2' => array( 'post_type' => 'page' ),
 			),
-			'orderby empty'             => array(
+			'orderby empty'                                => array(
 				'query_vars1' => array( 'orderby' => null ),
 				'query_vars2' => array( 'orderby' => 'date' ),
 			),
-			'different order parameter' => array(
+			'different order parameter'                    => array(
 				'query_vars1' => array(
 					'post_type'      => 'post',
@@ -303,29 +475,239 @@
 				),
 			),
-			'same args'                 => array(
+			'same args'                                    => array(
 				'query_vars1' => array( 'post_type' => 'post' ),
 				'query_vars2' => array( 'post_type' => 'post' ),
 			),
-			'same args any'             => array(
+			'same args any'                                => array(
 				'query_vars1' => array( 'post_type' => 'any' ),
 				'query_vars2' => array( 'post_type' => 'any' ),
 			),
-			'any and post types'        => array(
+			'any and post types'                           => array(
 				'query_vars1' => array( 'post_type' => 'any' ),
 				'query_vars2' => array( 'post_type' => array( 'post', 'page', 'attachment' ) ),
 			),
-			'different order post type' => array(
+			'different order post type'                    => array(
 				'query_vars1' => array( 'post_type' => array( 'post', 'page' ) ),
 				'query_vars2' => array( 'post_type' => array( 'page', 'post' ) ),
 			),
-			'post status array'         => array(
+			'non-unique post type'                         => array(
+				'query_vars1' => array( 'post_type' => array( 'post', 'page' ) ),
+				'query_vars2' => array( 'post_type' => array( 'page', 'post', 'page' ) ),
+			),
+			'post status array'                            => array(
 				'query_vars1' => array( 'post_status' => 'publish' ),
 				'query_vars2' => array( 'post_status' => array( 'publish' ) ),
 			),
-			'post status order'         => array(
+			'post status order'                            => array(
 				'query_vars1' => array( 'post_status' => array( 'draft', 'publish' ) ),
 				'query_vars2' => array( 'post_status' => array( 'publish', 'draft' ) ),
 			),
-			'cache parameters'          => array(
+			'non-unique post status'                       => array(
+				'query_vars1' => array( 'post_status' => array( 'draft', 'publish' ) ),
+				'query_vars2' => array( 'post_status' => array( 'draft', 'publish', 'draft' ) ),
+			),
+			'post id int vs string'                        => array(
+				'query_vars1' => array( 'p' => '1' ),
+				'query_vars2' => array( 'p' => 1 ),
+			),
+			'page id int vs string'                        => array(
+				'query_vars1' => array( 'page_id' => '2' ),
+				'query_vars2' => array( 'page_id' => 2 ),
+			),
+			'attachment id int vs string'                  => array(
+				'query_vars1' => array( 'attachment_id' => '3' ),
+				'query_vars2' => array( 'attachment_id' => 3 ),
+			),
+			'date and time values int vs string'           => array(
+				'query_vars1' => array(
+					'year'     => '2013',
+					'monthnum' => '12',
+					'day'      => '12',
+					'hour'     => '12',
+					'minute'   => '12',
+					'second'   => '12',
+				),
+				'query_vars2' => array(
+					'year'     => 2013,
+					'monthnum' => 12,
+					'day'      => 12,
+					'hour'     => 12,
+					'minute'   => 12,
+					'second'   => 12,
+				),
+			),
+			'offset value int vs string'                   => array(
+				'query_vars1' => array( 'offset' => '5' ),
+				'query_vars2' => array( 'offset' => 5 ),
+			),
+			'posts per page value int vs string'           => array(
+				'query_vars1' => array( 'posts_per_page' => '5' ),
+				'query_vars2' => array( 'posts_per_page' => 5 ),
+			),
+			'paged value int vs string'                    => array(
+				'query_vars1' => array( 'paged' => '2' ),
+				'query_vars2' => array( 'paged' => 2 ),
+			),
+			'menu_order value int vs string'               => array(
+				'query_vars1' => array( 'menu_order' => '2' ),
+				'query_vars2' => array( 'menu_order' => 2 ),
+			),
+			'post__in different order'                     => array(
+				'query_vars1' => array( 'post__in' => array( 1, 2, 3, 4, 5 ) ),
+				'query_vars2' => array( 'post__in' => array( 5, 4, 3, 2, 1 ) ),
+			),
+			'post__in non-unique'                          => array(
+				'query_vars1' => array( 'post__in' => array( 1, 2, 3, 4, 5 ) ),
+				'query_vars2' => array( 'post__in' => array( 1, 2, 3, 4, 5, 1, 2, 3 ) ),
+			),
+			'post_parent__in different order'              => array(
+				'query_vars1' => array( 'post_parent__in' => array( 1, 2, 3, 4, 5 ) ),
+				'query_vars2' => array( 'post_parent__in' => array( 5, 4, 3, 2, 1 ) ),
+			),
+			'post_parent__in non-unique'                   => array(
+				'query_vars1' => array( 'post_parent__in' => array( 1, 2, 3, 4, 5 ) ),
+				'query_vars2' => array( 'post_parent__in' => array( 1, 2, 3, 4, 5, 1, 2, 3 ) ),
+			),
+			'post_name__in different order'                => array(
+				'query_vars1' => array( 'post_name__in' => array( 'elphaba', 'glinda', 'the-wizard-of-oz', 'doctor-dillamond' ) ),
+				'query_vars2' => array( 'post_name__in' => array( 'doctor-dillamond', 'elphaba', 'the-wizard-of-oz', 'glinda' ) ),
+			),
+			'post_name__in non-unique'                     => array(
+				'query_vars1' => array( 'post_name__in' => array( 'elphaba', 'glinda', 'the-wizard-of-oz', 'doctor-dillamond' ) ),
+				'query_vars2' => array( 'post_name__in' => array( 'elphaba', 'glinda', 'elphaba', 'glinda', 'the-wizard-of-oz', 'doctor-dillamond' ) ),
+			),
+			'cat different order (array)'                  => array(
+				'query_vars_1' => array( 'cat' => array( '1', '2' ) ),
+				'query_vars_2' => array( 'cat' => array( '2', '1' ) ),
+			),
+			'cat different order (string)'                 => array(
+				'query_vars_1' => array( 'cat' => '2,1' ),
+				'query_vars_2' => array( 'cat' => '1,2' ),
+			),
+			'cat queries int vs string'                    => array(
+				'query_vars_1' => array( 'cat' => '2' ),
+				'query_vars_2' => array( 'cat' => 2 ),
+			),
+			'category__in queries different order (array)' => array(
+				'query_vars_1' => array( 'category__in' => array( '1', '2' ) ),
+				'query_vars_2' => array( 'category__in' => array( '2', '1' ) ),
+			),
+			'category__in queries with non-unique array'   => array(
+				'query_vars_1' => array( 'category__in' => array( '1', '1' ) ),
+				'query_vars_2' => array( 'category__in' => array( '1' ) ),
+			),
+			'category__in queries string vs array (array)' => array(
+				'query_vars_1' => array( 'category__in' => array( '1' ) ),
+				'query_vars_2' => array( 'category__in' => array( 1 ) ),
+			),
+			'category__not_in different order (array)'     => array(
+				'query_vars_1' => array( 'category__not_in' => array( '1', '2' ) ),
+				'query_vars_2' => array( 'category__not_in' => array( '2', '1' ) ),
+			),
+			'category__not_in with non-unique array'       => array(
+				'query_vars_1' => array( 'category__not_in' => array( '1', '1' ) ),
+				'query_vars_2' => array( 'category__not_in' => array( '1' ) ),
+			),
+			'category__not_in queries string vs array (array)' => array(
+				'query_vars_1' => array( 'category__not_in' => array( '1' ) ),
+				'query_vars_2' => array( 'category__not_in' => array( 1 ) ),
+			),
+			'category__and queries width different order (array)' => array(
+				'query_vars_1' => array( 'category__and' => array( '1', '2' ) ),
+				'query_vars_2' => array( 'category__and' => array( '2', '1' ) ),
+			),
+			'category__and with non-unique array'          => array(
+				'query_vars_1' => array( 'category__and' => array( '1', '1', '2' ) ),
+				'query_vars_2' => array( 'category__and' => array( '1', '2' ) ),
+			),
+			'category__and queries string vs array (array)' => array(
+				'query_vars_1' => array( 'category__and' => array( '1', '2' ) ),
+				'query_vars_2' => array( 'category__and' => array( 1, 2 ) ),
+			),
+			'author queries different order (string)'      => array(
+				'query_vars_1' => array( 'author' => '1,2' ),
+				'query_vars_2' => array( 'author' => '2,1' ),
+			),
+			'author with non-unique string'                => array(
+				'query_vars_1' => array( 'author' => '1,1' ),
+				'query_vars_2' => array( 'author' => '1' ),
+			),
+			'author queries int vs string (string)'        => array(
+				'query_vars_1' => array( 'author' => 1 ),
+				'query_vars_2' => array( 'author' => '1' ),
+			),
+			'author queries int vs string (array)'         => array(
+				'query_vars_1' => array( 'author' => array( 1 ) ),
+				'query_vars_2' => array( 'author' => array( '1' ) ),
+			),
+			'author__in different order'                   => array(
+				'query_vars_1' => array( 'author__in' => array( 1, 2 ) ),
+				'query_vars_2' => array( 'author__in' => array( 2, 1 ) ),
+			),
+			'author__in with non-unique array'             => array(
+				'query_vars_1' => array( 'author__in' => array( 1, 1, 2 ) ),
+				'query_vars_2' => array( 'author__in' => array( 1, 2 ) ),
+			),
+			'author__in queries int vs string (array)'     => array(
+				'query_vars_1' => array( 'author__in' => array( 1 ) ),
+				'query_vars_2' => array( 'author__in' => array( '1' ) ),
+			),
+			'author__not_in different order (array)'       => array(
+				'query_vars_1' => array( 'author__not_in' => array( 1, 2 ) ),
+				'query_vars_2' => array( 'author__not_in' => array( 2, 1 ) ),
+			),
+			'author__not_in queries int vs string (array)' => array(
+				'query_vars_1' => array( 'author__not_in' => array( 1 ) ),
+				'query_vars_2' => array( 'author__not_in' => array( '1' ) ),
+			),
+			'tag_slug__in order'                           => array(
+				'query_vars_1' => array( 'tag_slug__in' => array( 'foo', 'bar' ) ),
+				'query_vars_2' => array( 'tag_slug__in' => array( 'bar', 'foo' ) ),
+			),
+			'tag_slug__in non-unique vs unique'            => array(
+				'query_vars_1' => array( 'tag_slug__in' => array( 'foo', 'bar', 'bar' ) ),
+				'query_vars_2' => array( 'tag_slug__in' => array( 'foo', 'bar' ) ),
+			),
+			'tag_slug__and order'                          => array(
+				'query_vars_1' => array( 'tag_slug__and' => array( 'foo', 'bar' ) ),
+				'query_vars_2' => array( 'tag_slug__and' => array( 'bar', 'foo' ) ),
+			),
+			'tag_slug__and non-unique'                     => array(
+				'query_vars_1' => array( 'tag_slug__and' => array( 'foo', 'bar', 'foo' ) ),
+				'query_vars_2' => array( 'tag_slug__and' => array( 'bar', 'foo' ) ),
+			),
+			'tag__in queries different order (array)'      => array(
+				'query_vars_1' => array( 'tag__in' => array( 1, 2 ) ),
+				'query_vars_2' => array( 'tag__in' => array( 2, 1 ) ),
+			),
+			'tag__in queries non-unique array'             => array(
+				'query_vars_1' => array( 'tag__in' => array( 1, 2, 1 ) ),
+				'query_vars_2' => array( 'tag__in' => array( 2, 1 ) ),
+			),
+			'tag__in queries int vs string'                => array(
+				'query_vars_1' => array( 'tag__in' => array( 2, 1 ) ),
+				'query_vars_2' => array( 'tag__in' => array( '2', '1' ) ),
+			),
+			'tag__and queries different order (array)'     => array(
+				'query_vars_1' => array( 'tag__and' => array( 1, 2 ) ),
+				'query_vars_2' => array( 'tag__and' => array( 2, 1 ) ),
+			),
+			'tag__and queries non-unique array'            => array(
+				'query_vars_1' => array( 'tag__and' => array( 1, 2, 2 ) ),
+				'query_vars_2' => array( 'tag__and' => array( 2, 1 ) ),
+			),
+			'tag__not_in queries different order (array)'  => array(
+				'query_vars_1' => array( 'tag__not_in' => array( 1, 2 ) ),
+				'query_vars_2' => array( 'tag__not_in' => array( 2, 1 ) ),
+			),
+			'tag__not_in queries non-unique array'         => array(
+				'query_vars_1' => array( 'tag__not_in' => array( 1, 2, 2 ) ),
+				'query_vars_2' => array( 'tag__not_in' => array( 1, 2 ) ),
+			),
+			'tag__not_in queries int vs string (array)'    => array(
+				'query_vars_1' => array( 'tag__not_in' => array( '1' ) ),
+				'query_vars_2' => array( 'tag__not_in' => array( 1 ) ),
+			),
+			'cache parameters'                             => array(
 				'query_vars1' => array(
 					'update_post_meta_cache' => true,
Index: /trunk/tests/phpunit/tests/query/parseQuery.php
===================================================================
--- /trunk/tests/phpunit/tests/query/parseQuery.php	(revision 59765)
+++ /trunk/tests/phpunit/tests/query/parseQuery.php	(revision 59766)
@@ -152,5 +152,5 @@
 		);
 
-		$this->assertSame( '1,-1', $q->query_vars['cat'] );
+		$this->assertSame( '-1,1', $q->query_vars['cat'] );
 	}
 
Index: /trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
===================================================================
--- /trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php	(revision 59765)
+++ /trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php	(revision 59766)
@@ -1575,5 +1575,5 @@
 		$this->assertNotContains( $id3, $ids );
 
-		$this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
+		$this->assertPostsWhere( " AND {posts}.ID NOT IN ($id2,$id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
 	}
 
