Make WordPress Core


Ignore:
Timestamp:
02/10/2025 10:21:51 PM (3 months ago)
Author:
peterwilsoncc
Message:

REST API: Add support for the ignore_sticky_posts argument.

Introduce ignore_sticky as a boolean argument for the posts endpoint for requests without the sticky posts being stuck. The new argument defaults to false with the value of the argument passed to WP_Query's ignore_sticky_posts parameter.

Props audrasjb, danielbachhuber, joemcgill, johnbillion, jorbin, mamaduka, rmccue.
Fixes #35907.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r59766 r59801  
    198198                'exclude',
    199199                'format',
     200                'ignore_sticky',
    200201                'include',
    201202                'modified_after',
     
    57175718
    57185719    /**
     5720     * Test the REST API support for `ignore_sticky_posts`.
     5721     *
     5722     * @ticket 35907
     5723     *
     5724     * @covers WP_REST_Posts_Controller::get_items
     5725     */
     5726    public function test_get_posts_ignore_sticky_default_prepends_sticky_posts() {
     5727        $id1 = self::$post_id;
     5728        // Create more recent post to avoid automatically placing other at the top.
     5729        $id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     5730
     5731        update_option( 'sticky_posts', array( $id1 ) );
     5732
     5733        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5734        $response = rest_get_server()->dispatch( $request );
     5735        $data     = $response->get_data();
     5736
     5737        $this->assertSame( $data[0]['id'], $id1, 'Response has sticky post at the top.' );
     5738        $this->assertSame( $data[1]['id'], $id2, 'It is followed by most recent post.' );
     5739    }
     5740
     5741    /**
     5742     * Test the REST API support for `ignore_sticky_posts`.
     5743     *
     5744     * @ticket 35907
     5745     *
     5746     * @covers WP_REST_Posts_Controller::get_items
     5747     */
     5748    public function test_get_posts_ignore_sticky_ignores_post_stickiness() {
     5749        $id1 = self::$post_id;
     5750        $id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     5751
     5752        update_option( 'sticky_posts', array( $id1 ) );
     5753
     5754        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5755        $request->set_param( 'ignore_sticky', true );
     5756        $response = rest_get_server()->dispatch( $request );
     5757        $data     = $response->get_data();
     5758
     5759        $this->assertSame( $data[0]['id'], $id2, 'Response has no sticky post at the top.' );
     5760    }
     5761
     5762    /**
     5763     * Test the REST API support for `ignore_sticky_posts`.
     5764     *
     5765     * @ticket 35907
     5766     *
     5767     * @covers WP_REST_Posts_Controller::get_items
     5768     */
     5769    public function test_get_posts_ignore_sticky_honors_include() {
     5770        $id1 = self::$post_id;
     5771        $id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     5772
     5773        update_option( 'sticky_posts', array( $id1 ) );
     5774
     5775        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5776        $request->set_param( 'include', array( $id2 ) );
     5777        $response = rest_get_server()->dispatch( $request );
     5778        $data     = $response->get_data();
     5779
     5780        $this->assertCount( 1, $data, 'Only one post is expected to be returned.' );
     5781        $this->assertSame( $data[0]['id'], $id2, 'Returns the included post.' );
     5782    }
     5783
     5784    /**
    57195785     * Internal function used to disable an insert query which
    57205786     * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.