#29167 closed defect (bug) (fixed)
WP_Query bug when querying trashed post
Reported by: | ebinnion | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.2 | Priority: | normal |
Severity: | normal | Version: | 3.8 |
Component: | Query | Keywords: | has-patch |
Focuses: | Cc: |
Description
While working with Backbone and polling the server to check for changes to posts, I came across what I consider to be a bug.
It seems that when specifying a post to retrieve, if the post is trashed, then the WP_Query object will contain no posts. However, if I was to query for trashed posts, a trashed post would show.
I was able to replicate this bug against the trunk version with the following two queries.
This query will return the trashed post I have in the database. Here is a dump of the WP_Query object: http://pastebin.com/zi50DKGt
$trashed = new WP_Query( array( 'post_status' => 'trash', ) );
This query does not return the trashed post. Here is the dumped WP_Query object for the query below: http://pastebin.com/MRf8gYj1
$trashed = new WP_Query( array( 'p' => 4 ) );
Up to this point, I could understand this behavior since it would make sense to not show a post that was trashed. But, if the query specifically requests the post by ID and specifies that the post_status is trashed, I would expect the trashed post to be retrieved. That is not the case.
$trashed = new WP_Query( array( 'post_status' => 'trash', 'p' => 4 ) );
Here is the dumped WP_Query object for the above query: http://pastebin.com/1PgTg7e2
Attachments (1)
Change History (10)
@
10 years ago
First patch for returning posts that may not be public if the correct post_status
has been set.
#2
@
10 years ago
- Keywords has-patch needs-testing dev-feedback added
I have attached a patch that fixes this issue in my informal tests, although I have not yet run this through PHPUnit.
#3
@
10 years ago
- Keywords needs-testing removed
I have verified that the all query group tests pass, or are skipped. Also, I have created a unit test to verify the presence of the bug before and the absence of the bug after the suggested patch.
Here's a screenshot of the unit tests I ran. The first test is without the suggested patch. The second test is with the suggested patch.
Also, here is the unit test I came up with to test querying for a specific trashed post.
<?php /** * Tests to make sure that trashed posts can be queried when post_status is set to 'trash'. * * @group query * */ class Tests_Trashed_Post_Query extends WP_UnitTestCase { public function setUp() { parent::setUp(); $this->post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_status' => 'trash' ) ); } public function test_query_trash() { $post = new WP_Query( array( 'p' => $this->post_id, 'post_status' => 'trash' ) ); $this->assertCount( 1, $post->posts ); } }
#5
@
10 years ago
I've confirmed the bug. The basic strategy here - whitelisting posts if their post_status was explicitly requested - looks good, but I think we'll need a bit more logic to account for all the ways that the post_status clauses can be built ('any', etc). I'll start by writing some unit tests.
#8
@
10 years ago
- Owner set to boonebgorges
- Resolution set to fixed
- Status changed from new to closed
In 31321:
Tangentially related #21660