Skip to content

Commit

Permalink
REST API: Add support for the ignore_sticky_posts argument.
Browse files Browse the repository at this point in the history
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.



git-svn-id: https://develop.svn.wordpress.org/trunk@59801 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Feb 10, 2025
1 parent 9160fbd commit f71d5f0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public function get_items( $request ) {
'author_exclude' => 'author__not_in',
'exclude' => 'post__not_in',
'include' => 'post__in',
'ignore_sticky' => 'ignore_sticky_posts',
'menu_order' => 'menu_order',
'offset' => 'offset',
'order' => 'order',
Expand Down Expand Up @@ -337,6 +338,14 @@ public function get_items( $request ) {
}
}

/*
* Honor the original REST API `post__in` behavior. Don't prepend sticky posts
* when `post__in` has been specified.
*/
if ( ! empty( $args['post__in'] ) ) {
unset( $args['ignore_sticky_posts'] );
}

if (
isset( $registered['search_semantics'], $request['search_semantics'] )
&& 'exact' === $request['search_semantics']
Expand Down Expand Up @@ -3045,6 +3054,12 @@ public function get_collection_params() {
'description' => __( 'Limit result set to items that are sticky.' ),
'type' => 'boolean',
);

$query_params['ignore_sticky'] = array(
'description' => __( 'Whether to ignore sticky posts or not.' ),
'type' => 'boolean',
'default' => false,
);
}

if ( post_type_supports( $this->post_type, 'post-formats' ) ) {
Expand Down
66 changes: 66 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public function test_registered_query_params() {
'context',
'exclude',
'format',
'ignore_sticky',
'include',
'modified_after',
'modified_before',
Expand Down Expand Up @@ -5715,6 +5716,71 @@ public function test_get_posts_with_pagination() {
$this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 );
}

/**
* Test the REST API support for `ignore_sticky_posts`.
*
* @ticket 35907
*
* @covers WP_REST_Posts_Controller::get_items
*/
public function test_get_posts_ignore_sticky_default_prepends_sticky_posts() {
$id1 = self::$post_id;
// Create more recent post to avoid automatically placing other at the top.
$id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );

update_option( 'sticky_posts', array( $id1 ) );

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( $data[0]['id'], $id1, 'Response has sticky post at the top.' );
$this->assertSame( $data[1]['id'], $id2, 'It is followed by most recent post.' );
}

/**
* Test the REST API support for `ignore_sticky_posts`.
*
* @ticket 35907
*
* @covers WP_REST_Posts_Controller::get_items
*/
public function test_get_posts_ignore_sticky_ignores_post_stickiness() {
$id1 = self::$post_id;
$id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );

update_option( 'sticky_posts', array( $id1 ) );

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'ignore_sticky', true );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( $data[0]['id'], $id2, 'Response has no sticky post at the top.' );
}

/**
* Test the REST API support for `ignore_sticky_posts`.
*
* @ticket 35907
*
* @covers WP_REST_Posts_Controller::get_items
*/
public function test_get_posts_ignore_sticky_honors_include() {
$id1 = self::$post_id;
$id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );

update_option( 'sticky_posts', array( $id1 ) );

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'include', array( $id2 ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertCount( 1, $data, 'Only one post is expected to be returned.' );
$this->assertSame( $data[0]['id'], $id2, 'Returns the included post.' );
}

/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.
Expand Down
6 changes: 6 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,12 @@ mockedApiResponse.Schema = {
"type": "boolean",
"required": false
},
"ignore_sticky": {
"description": "Whether to ignore sticky posts or not.",
"type": "boolean",
"default": false,
"required": false
},
"format": {
"description": "Limit result set to items assigned one or more given formats.",
"type": "array",
Expand Down

0 comments on commit f71d5f0

Please sign in to comment.