REST API: Fix behavior of `sticky` posts filter when no posts are sticky.

Previously, when getting posts from the API with `sticky=true`, if there were no sticky posts set, the query would return all posts  as if the `sticky` argument was not set.  In this situation, the query should return an empty array instead.

A `sticky=true` query that should return an empty array (in the previous situation, or with `include` and no intersecting post IDs) was also broken in that it would query the post with ID 1.

Finally, this commit significantly improves test coverage for the `sticky` filter argument, including direct testing of the `WHERE` clauses generated by `WP_Query`.

Props ryelle.
Fixes #39947.

Built from https://develop.svn.wordpress.org/trunk@40122


git-svn-id: http://core.svn.wordpress.org/trunk@40059 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
James Nylen 2017-02-25 05:03:42 +00:00
parent a5ad623bfd
commit 0c005fdb0e
2 changed files with 6 additions and 3 deletions

View File

@ -220,7 +220,10 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
if ( isset( $registered['sticky'], $request['sticky'] ) ) { if ( isset( $registered['sticky'], $request['sticky'] ) ) {
$sticky_posts = get_option( 'sticky_posts', array() ); $sticky_posts = get_option( 'sticky_posts', array() );
if ( $sticky_posts && $request['sticky'] ) { if ( ! is_array( $sticky_posts ) ) {
$sticky_posts = array();
}
if ( $request['sticky'] ) {
/* /*
* As post__in will be used to only get sticky posts, * As post__in will be used to only get sticky posts,
* we have to support the case where post__in was already * we have to support the case where post__in was already
@ -234,7 +237,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
* so we have to fake it a bit. * so we have to fake it a bit.
*/ */
if ( ! $args['post__in'] ) { if ( ! $args['post__in'] ) {
$args['post__in'] = array( -1 ); $args['post__in'] = array( 0 );
} }
} elseif ( $sticky_posts ) { } elseif ( $sticky_posts ) {
/* /*

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.8-alpha-40121'; $wp_version = '4.8-alpha-40122';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.