WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#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:
PR Number:

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)

29167.diff (587 bytes) - added by ebinnion 5 years ago.
First patch for returning posts that may not be public if the correct post_status has been set.

Download all attachments as: .zip

Change History (9)

#1 @danielbachhuber
5 years ago

Tangentially related #21660

@ebinnion
5 years ago

First patch for returning posts that may not be public if the correct post_status has been set.

#2 @ebinnion
5 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 @ebinnion
5 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.

https://i.cloudup.com/dN6SQuzcaE.png

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 );
	}

}

#4 @tellyworth
5 years ago

  • Version changed from trunk to 3.8

#5 @boonebgorges
5 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.

#6 @boonebgorges
5 years ago

In 31047:

Add tests for the 'post_status' behavior of WP_Query.

See #29167.

#7 @boonebgorges
5 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Awaiting Review to 4.2

#8 @boonebgorges
5 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 31321:

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.

Note: See TracTickets for help on using tickets.