WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/25/2017 05:02:17 AM (3 years ago)
Author:
jnylen0
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r40120 r40122  
    221221        if ( isset( $registered['sticky'], $request['sticky'] ) ) {
    222222            $sticky_posts = get_option( 'sticky_posts', array() );
    223             if ( $sticky_posts && $request['sticky'] ) {
     223            if ( ! is_array( $sticky_posts ) ) {
     224                $sticky_posts = array();
     225            }
     226            if ( $request['sticky'] ) {
    224227                /*
    225228                 * As post__in will be used to only get sticky posts,
     
    235238                 */
    236239                if ( ! $args['post__in'] ) {
    237                     $args['post__in'] = array( -1 );
     240                    $args['post__in'] = array( 0 );
    238241                }
    239242            } elseif ( $sticky_posts ) {
Note: See TracChangeset for help on using the changeset viewer.