Opened 11 years ago
Closed 11 years ago
#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)
Change History (9)
#3
in reply to:
↑ 2
;
follow-up:
↓ 4
@
11 years 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
#4
in reply to:
↑ 3
@
11 years ago
Replying to helen:
Awesome, missed that bit while reading that. Thanks! I'll work in some tests for this.
@
11 years 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.
#5
@
11 years 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.
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.