From 5f6ea53e869ac7903976983c486c5a809b2f9f6b Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 1 Feb 2015 20:25:42 +0000 Subject: [PATCH] When querying for a specific post, allow posts with a non-public status to be returned as long as that status is specified. This makes it possible to, for example, retrieve a specific post using the `p` parameter of `WP_Query`, even if the post is in the Trash, by including the `post_status=trash` parameter. Props ebinnion. Fixes #29167. git-svn-id: https://develop.svn.wordpress.org/trunk@31321 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 6 +++++- tests/phpunit/tests/query/postStatus.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 6a5aceaa80..3c1b0c2bdb 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2963,6 +2963,7 @@ class WP_Query { $user_id = get_current_user_id(); + $q_status = array(); if ( ! empty( $q['post_status'] ) ) { $statuswheres = array(); $q_status = $q['post_status']; @@ -3527,7 +3528,10 @@ class WP_Query { $status = get_post_status($this->posts[0]); $post_status_obj = get_post_status_object($status); //$type = get_post_type($this->posts[0]); - if ( !$post_status_obj->public ) { + + // If the post_status was specifically requested, let it pass through. + if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) { + if ( ! is_user_logged_in() ) { // User must be logged in to view unpublished posts. $this->posts = array(); diff --git a/tests/phpunit/tests/query/postStatus.php b/tests/phpunit/tests/query/postStatus.php index 4264eb7cd3..bcc7fbad60 100644 --- a/tests/phpunit/tests/query/postStatus.php +++ b/tests/phpunit/tests/query/postStatus.php @@ -298,4 +298,19 @@ class Tests_Query_PostStatus extends WP_UnitTestCase { $this->assertEmpty( $q->posts ); } + + /** + * @ticket 29167 + */ + public function test_specific_post_should_be_returned_if_trash_is_one_of_the_requested_post_statuses() { + $p1 = $this->factory->post->create( array( 'post_status' => 'trash' ) ); + $p2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + + $q = new WP_Query( array( + 'p' => $p1, + 'post_status' => array( 'trash', 'publish' ), + ) ); + + $this->assertContains( $p1, wp_list_pluck( $q->posts, 'ID' ) ); + } }