From 8bb2117aadc9994ca5d6ceb6579746ed96f62468 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 29 May 2014 20:41:53 +0000 Subject: [PATCH] If `post_status` is passed to `WP_Query` as an array containing `'any'` and anything else, don't exclude the other values if they match when running `any`'s exclusion logic. Adds unit tests. Fixes #28007. git-svn-id: https://develop.svn.wordpress.org/trunk@28622 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 9 ++++++--- tests/phpunit/tests/post/query.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 2dcc95c949..ddf67d564e 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2767,9 +2767,12 @@ class WP_Query { $r_status = array(); $p_status = array(); $e_status = array(); - if ( in_array('any', $q_status) ) { - foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status ) - $e_status[] = "$wpdb->posts.post_status <> '$status'"; + if ( in_array( 'any', $q_status ) ) { + foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { + if ( ! in_array( $status, $q_status ) ) { + $e_status[] = "$wpdb->posts.post_status <> '$status'"; + } + } } else { foreach ( get_post_stati() as $status ) { if ( in_array( $status, $q_status ) ) { diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index ee7d64332d..37dcdc7517 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -741,4 +741,24 @@ class Tests_Post_Query extends WP_UnitTestCase { ) ); $this->assertEqualSets( $ordered, wp_list_pluck( $attached->posts, 'ID' ) ); } + + function test_post_status() { + $statuses1 = get_post_stati(); + $this->assertContains( 'auto-draft', $statuses1 ); + + $statuses2 = get_post_stati( array( 'exclude_from_search' => true ) ); + $this->assertContains( 'auto-draft', $statuses2 ); + + $statuses3 = get_post_stati( array( 'exclude_from_search' => false ) ); + $this->assertNotContains( 'auto-draft', $statuses3 ); + + $q1 = new WP_Query( array( 'post_status' => 'any' ) ); + $this->assertContains( "post_status <> 'auto-draft'", $q1->request ); + + $q2 = new WP_Query( array( 'post_status' => 'any, auto-draft' ) ); + $this->assertNotContains( "post_status <> 'auto-draft'", $q2->request ); + + $q3 = new WP_Query( array( 'post_status' => array( 'any', 'auto-draft' ) ) ); + $this->assertNotContains( "post_status <> 'auto-draft'", $q3->request ); + } } \ No newline at end of file