Make WordPress Core

Changeset 60197


Ignore:
Timestamp:
04/28/2025 04:03:59 PM (15 months ago)
Author:
jorbin
Message:

REST API: Change posts endpoint to ignore_sticky=true by default

This restores the 6.7 and below behavior for the posts endpoint which did not include sticky posts by default.

Follow-up to [59801].

Props nikunj8866, SirLouen, ankitmaru, wildworks, karthikeya01, Mamaduka, spacedmonkey, jorbin.
Fixes #63307. See #35907.

Location:
trunk
Files:
3 edited

Legend:

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

    r59970 r60197  
    30763076                                'description' => __( 'Whether to ignore sticky posts or not.' ),
    30773077                                'type'        => 'boolean',
    3078                                 'default'     => false,
     3078                                'default'     => true,
    30793079                        );
    30803080                }
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r59985 r60197  
    662662        }
    663663
     664        /**
     665         * @ticket 63307
     666         */
    664667        public function test_get_items_slug_query() {
    665                 self::factory()->post->create(
     668                $id1 = self::factory()->post->create(
    666669                        array(
    667670                                'post_title'  => 'Apple',
     
    669672                        )
    670673                );
    671                 self::factory()->post->create(
     674                $id2 = self::factory()->post->create(
    672675                        array(
    673676                                'post_title'  => 'Banana',
     
    675678                        )
    676679                );
     680
     681                update_option( 'sticky_posts', array( $id2 ) );
    677682
    678683                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     
    682687                $data = $response->get_data();
    683688                $this->assertCount( 1, $data );
    684                 $this->assertSame( 'Apple', $data[0]['title']['rendered'] );
     689                $this->assertSame( 'Apple', $data[0]['title']['rendered'], 'Return the post with the given slug' );
    685690        }
    686691
     
    59715976
    59725977        /**
     5978         * Test the REST API doesn't prioritize sticky posts by default.
     5979         *
     5980         * @ticket 35907
     5981         * @ticket 63307
     5982         *
     5983         * @covers WP_REST_Posts_Controller::get_items
     5984         */
     5985        public function test_get_posts_ignore_sticky_by_default() {
     5986                $id1 = self::$post_id;
     5987                // Create more recent post to avoid automatically placing other at the top.
     5988                $id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     5989
     5990                update_option( 'sticky_posts', array( $id1 ) );
     5991
     5992                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5993                $response = rest_get_server()->dispatch( $request );
     5994                $data     = $response->get_data();
     5995                $rest_ids = wp_list_pluck( $data, 'id' );
     5996
     5997                $this->assertSame( $data[0]['id'], $id2, 'Response has no sticky post at the top.' );
     5998
     5999                $posts_query = new WP_Query( array( 'ignore_sticky_posts' => true ) );
     6000                $post_ids    = wp_list_pluck( $posts_query->get_posts(), 'ID' );
     6001                $this->assertSame( $rest_ids, $post_ids, 'Response is same as WP_Query with ignore_sticky_posts=true.' );
     6002        }
     6003
     6004        /**
    59736005         * Test the REST API support for `ignore_sticky_posts`.
    59746006         *
    59756007         * @ticket 35907
     6008         * @ticket 63307
    59766009         *
    59776010         * @covers WP_REST_Posts_Controller::get_items
    59786011         */
    5979         public function test_get_posts_ignore_sticky_default_prepends_sticky_posts() {
     6012        public function test_get_posts_ignore_sticky_false_prepends_sticky_posts() {
    59806013                $id1 = self::$post_id;
    59816014                // Create more recent post to avoid automatically placing other at the top.
     
    59846017                update_option( 'sticky_posts', array( $id1 ) );
    59856018
    5986                 $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    5987                 $response = rest_get_server()->dispatch( $request );
    5988                 $data     = $response->get_data();
     6019                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     6020                $request->set_param( 'ignore_sticky', false );
     6021                $response = rest_get_server()->dispatch( $request );
     6022                $data     = $response->get_data();
     6023                $rest_ids = wp_list_pluck( $data, 'id' );
    59896024
    59906025                $this->assertSame( $data[0]['id'], $id1, 'Response has sticky post at the top.' );
    59916026                $this->assertSame( $data[1]['id'], $id2, 'It is followed by most recent post.' );
     6027
     6028                $posts_query = new WP_Query();
     6029                $post_ids    = wp_list_pluck( $posts_query->get_posts(), 'ID' );
     6030                $this->assertSame( $rest_ids, $post_ids, 'Response is same as WP_Query with ignore_sticky_posts=false.' );
    59926031        }
    59936032
     
    59966035         *
    59976036         * @ticket 35907
     6037         * @ticket 63307
    59986038         *
    59996039         * @covers WP_REST_Posts_Controller::get_items
    60006040         */
    6001         public function test_get_posts_ignore_sticky_ignores_post_stickiness() {
     6041        public function test_get_posts_ignore_sticky_honors_include() {
     6042
    60026043                $id1 = self::$post_id;
    60036044                $id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     
    60066047
    60076048                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    6008                 $request->set_param( 'ignore_sticky', true );
    6009                 $response = rest_get_server()->dispatch( $request );
    6010                 $data     = $response->get_data();
    6011 
    6012                 $this->assertSame( $data[0]['id'], $id2, 'Response has no sticky post at the top.' );
    6013         }
    6014 
    6015         /**
    6016          * Test the REST API support for `ignore_sticky_posts`.
    6017          *
    6018          * @ticket 35907
    6019          *
    6020          * @covers WP_REST_Posts_Controller::get_items
    6021          */
    6022         public function test_get_posts_ignore_sticky_honors_include() {
    6023                 $id1 = self::$post_id;
    6024                 $id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    6025 
    6026                 update_option( 'sticky_posts', array( $id1 ) );
    6027 
    6028                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    60296049                $request->set_param( 'include', array( $id2 ) );
    60306050                $response = rest_get_server()->dispatch( $request );
    60316051                $data     = $response->get_data();
     6052                $rest_ids = wp_list_pluck( $data, 'id' );
    60326053
    60336054                $this->assertCount( 1, $data, 'Only one post is expected to be returned.' );
    60346055                $this->assertSame( $data[0]['id'], $id2, 'Returns the included post.' );
     6056
     6057                $posts_query = new WP_Query(
     6058                        array(
     6059                                'post__in'            => array( $id2 ),
     6060                                'ignore_sticky_posts' => true,
     6061                        )
     6062                );
     6063                $post_ids    = wp_list_pluck( $posts_query->get_posts(), 'ID' );
     6064                $this->assertSame( $rest_ids, $post_ids, 'Response is same as WP_Query with ignore_sticky_posts=truehas no sticky post at the top.' );
    60356065        }
    60366066
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r59892 r60197  
    634634                            "description": "Whether to ignore sticky posts or not.",
    635635                            "type": "boolean",
    636                             "default": false,
     636                            "default": true,
    637637                            "required": false
    638638                        },
Note: See TracChangeset for help on using the changeset viewer.