Skip to content

Commit 4e03672

Browse files
committed
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]. Reviewed by desrosj. Merges [60197] to the 6.8 branch. Props nikunj8866, SirLouen, ankitmaru, wildworks, karthikeya01, Mamaduka, spacedmonkey, jorbin. Fixes #63307. See #35907. git-svn-id: https://develop.svn.wordpress.org/branches/6.8@60200 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a5a2de5 commit 4e03672

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3075,7 +3075,7 @@ public function get_collection_params() {
30753075
$query_params['ignore_sticky'] = array(
30763076
'description' => __( 'Whether to ignore sticky posts or not.' ),
30773077
'type' => 'boolean',
3078-
'default' => false,
3078+
'default' => true,
30793079
);
30803080
}
30813081

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -661,27 +661,32 @@ public function test_get_items_search_query() {
661661
$this->assertSame( 'Search Result', $data[0]['title']['rendered'] );
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',
668671
'post_status' => 'publish',
669672
)
670673
);
671-
self::factory()->post->create(
674+
$id2 = self::factory()->post->create(
672675
array(
673676
'post_title' => 'Banana',
674677
'post_status' => 'publish',
675678
)
676679
);
677680

681+
update_option( 'sticky_posts', array( $id2 ) );
682+
678683
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
679684
$request->set_param( 'slug', 'apple' );
680685
$response = rest_get_server()->dispatch( $request );
681686
$this->assertSame( 200, $response->get_status() );
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

687692
public function test_get_items_multiple_slugs_array_query() {
@@ -5970,13 +5975,14 @@ public function test_get_posts_with_pagination() {
59705975
}
59715976

59725977
/**
5973-
* Test the REST API support for `ignore_sticky_posts`.
5978+
* Test the REST API doesn't prioritize sticky posts by default.
59745979
*
59755980
* @ticket 35907
5981+
* @ticket 63307
59765982
*
59775983
* @covers WP_REST_Posts_Controller::get_items
59785984
*/
5979-
public function test_get_posts_ignore_sticky_default_prepends_sticky_posts() {
5985+
public function test_get_posts_ignore_sticky_by_default() {
59805986
$id1 = self::$post_id;
59815987
// Create more recent post to avoid automatically placing other at the top.
59825988
$id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
@@ -5986,40 +5992,54 @@ public function test_get_posts_ignore_sticky_default_prepends_sticky_posts() {
59865992
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
59875993
$response = rest_get_server()->dispatch( $request );
59885994
$data = $response->get_data();
5995+
$rest_ids = wp_list_pluck( $data, 'id' );
59895996

5990-
$this->assertSame( $data[0]['id'], $id1, 'Response has sticky post at the top.' );
5991-
$this->assertSame( $data[1]['id'], $id2, 'It is followed by most recent post.' );
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.' );
59926002
}
59936003

59946004
/**
59956005
* Test the REST API support for `ignore_sticky_posts`.
59966006
*
59976007
* @ticket 35907
6008+
* @ticket 63307
59986009
*
59996010
* @covers WP_REST_Posts_Controller::get_items
60006011
*/
6001-
public function test_get_posts_ignore_sticky_ignores_post_stickiness() {
6012+
public function test_get_posts_ignore_sticky_false_prepends_sticky_posts() {
60026013
$id1 = self::$post_id;
6014+
// Create more recent post to avoid automatically placing other at the top.
60036015
$id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
60046016

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

60076019
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
6008-
$request->set_param( 'ignore_sticky', true );
6020+
$request->set_param( 'ignore_sticky', false );
60096021
$response = rest_get_server()->dispatch( $request );
60106022
$data = $response->get_data();
6023+
$rest_ids = wp_list_pluck( $data, 'id' );
60116024

6012-
$this->assertSame( $data[0]['id'], $id2, 'Response has no sticky post at the top.' );
6025+
$this->assertSame( $data[0]['id'], $id1, 'Response has sticky post at the top.' );
6026+
$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.' );
60136031
}
60146032

60156033
/**
60166034
* Test the REST API support for `ignore_sticky_posts`.
60176035
*
60186036
* @ticket 35907
6037+
* @ticket 63307
60196038
*
60206039
* @covers WP_REST_Posts_Controller::get_items
60216040
*/
60226041
public function test_get_posts_ignore_sticky_honors_include() {
6042+
60236043
$id1 = self::$post_id;
60246044
$id2 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
60256045

@@ -6029,9 +6049,19 @@ public function test_get_posts_ignore_sticky_honors_include() {
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

60376067
/**

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ mockedApiResponse.Schema = {
633633
"ignore_sticky": {
634634
"description": "Whether to ignore sticky posts or not.",
635635
"type": "boolean",
636-
"default": false,
636+
"default": true,
637637
"required": false
638638
},
639639
"format": {

0 commit comments

Comments
 (0)