WordPress.org

Make WordPress Core

#25523 closed defect (bug) (fixed)

Bug in query creation for the status (if many)

Reported by: asakurayoh Owned by: wonderboymusic
Milestone: 3.9 Priority: normal
Severity: major Version: 3.8
Component: Query Keywords: has-patch
Focuses: Cc:

Description

The creation of the "where" for the status is buggy in wp-includes/query.php.
I was trying to get publish post and private post as describe in the doc (with a user not admin, but with the good capabilities):

http://codex.wordpress.org/Class_Reference/WP_Query


Show posts if user has the appropriate capability:

Display published and private posts, if the user has the appropriate capability:

$query = new WP_Query( array( 'post_status' => array( 'publish', 'private' ), 'perm' => 'readable' ) );

that was not working so I debug it and the resulting where for the query was a lot strange:

 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') AND (wp_posts.post_author = 2 AND (wp_posts.post_status = 'private'))

If you read it, it mean that a post need to have 2 status at the same time...
I found the place to change it in wp-includes/query.php (~line 2501):

	foreach ( $statuswheres as $statuswhere )
		$where .= " AND $statuswhere";

for:

	$where_status = implode(' OR ', $statuswheres);
	if(!empty($where_status))
		$where .= " AND ($where_status)";

That all! The query is then more logic AND working:

 AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish') OR (wp_posts.post_author = 2 AND (wp_posts.post_status = 'private')))

If someone can create the patch, as I know more git than svn... Wordpress on github, with pull-request, would be so more easy...

Thanks.

Attachments (3)

post-status-perm.25523.diff (706 bytes) - added by oso96_2000 19 months ago.
post-status-perm-with-test.25523.diff (1.3 KB) - added by oso96_2000 18 months ago.
Been a loong week. Ok, after trying for a while I think I've come with a simple test with almost no overhead that just tests this fix. The way the code is now it doesn't even return any post, after the fix, it returns the posts created in the setUp method. Hope it works.
25523.diff (1.9 KB) - added by wonderboymusic 18 months ago.

Download all attachments as: .zip

Change History (9)

comment:1 @wonderboymusic19 months ago

  • Milestone changed from Awaiting Review to 3.9

comment:2 follow-up: @oso96_200019 months ago

I uploaded the patch with the suggested change. Tried to make a test too, but I can't find how to run the test if the ticket isn't marked as fixed yet.

comment:3 in reply to: ↑ 2 ; follow-up: @helen19 months ago

Replying to oso96_2000:

Assuming it's skipping the test due to the use of the @ticket annotation, you can deliberately run it using the --group flag: http://make.wordpress.org/core/handbook/automated-testing/#running-specific-tests

comment:4 in reply to: ↑ 3 @oso96_200019 months ago

Replying to helen:

Awesome, missed that bit while reading that. Thanks! I'll work in some tests for this.

@oso96_200018 months ago

Been a loong week. Ok, after trying for a while I think I've come with a simple test with almost no overhead that just tests this fix. The way the code is now it doesn't even return any post, after the fix, it returns the posts created in the setUp method. Hope it works.

@wonderboymusic18 months ago

comment:5 @wonderboymusic18 months ago

  • Keywords has-patch added; needs-patch removed

This is indeed an error in SQL generation. 25523.diff fixes it and adds a unit test.

comment:6 @wonderboymusic18 months ago

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

In 27067:

Fix an error in SQL generation when perm is set and an array is passed for post_status. Adds unit test.

Props oso96_2000.
Fixes #25523.

Note: See TracTickets for help on using tickets.