Opened 8 years ago
Closed 8 years ago
#39947 closed defect (bug) (fixed)
REST API: Sticky post query returns unsticky posts when no posts are sticky
Reported by: | ryelle | Owned by: | jnylen0 |
---|---|---|---|
Milestone: | 4.7.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | REST API | Keywords: | has-patch has-unit-tests fixed-major |
Focuses: | Cc: |
Description
When getting sticky posts from the API (for example, site.com/wp-json/wp/v2/posts?sticky=true
), if there are no sticky posts set, the query just returns all posts — as if sticky
was not set. I was expecting an empty array. If there are sticky posts, it behaves as expected, only returning the sticky posts.
The source seems to be this check in WP_REST_Posts_Controller.
Attachments (2)
Change History (14)
#3
@
8 years ago
@jnylen0 I am +1 on getting this fixed as soon as possible, I milestoned for 4.7.4 because I thought we'd missed the cutoff -- but let's see what we can do :)
#4
@
8 years ago
I would say that we need to get it committed to trunk today or tomorrow at the very latest. A bit of a stretch, but potentially doable.
if there are no sticky posts set
I guess we need to know what get_option( 'sticky_posts' )
returns in this case, and what other values it might return.
#5
@
8 years ago
I've added a patch with the is_array check from this comment, plus some tests. I also changed $args['post__in'] = array( -1 );
to 0
, because wp_query runs absint over the post__in
array, and so was converting it to post ID 1.
This ticket was mentioned in Slack in #core by jnylen. View the logs.
8 years ago
#8
@
8 years ago
- Milestone changed from 4.7.4 to 4.7.3
- Owner set to jnylen0
- Status changed from new to assigned
I think the approach looks good, but there are a few more tests that could be added here and I want to look at it more closely. I'll try to land this today for 4.7.3.
#9
@
8 years ago
- Keywords commit added
- Status changed from assigned to accepted
39947.2.diff adds more test cases and also tests the WHERE
clauses generated by WP_Query
using a similar approach to #39079. This catches the IN(0)
fix on this ticket.
@kadamwhite what do you think about getting this into 4.7.3 if we can get it patched quickly enough?
@ryelle are you available to write a patch and unit tests for this change today?
As for the patch, I would suggest something like this: