Add sticky parameter to the posts endpoint.#2708
Conversation
This allows getting posts that are either "only sticky" for "not sticky" via `sticky=true` or `sticky=false`. This can also be used in conjuction with `include` and `exclude`.
|
Fixes #2705 |
| $response = $this->server->dispatch( $request ); | ||
| $new_data = $response->get_data(); | ||
| $this->assertEquals( 'Hello\\s', $new_data['content']['raw'] ); | ||
| } |
There was a problem hiding this comment.
This test appears to be unrelated to the rest of the PR, can you split it into a new one? :)
There was a problem hiding this comment.
Ohhhhhh that's why this is failing, doh! Didn't mean to commit this.
Current coverage is 94.47% (diff: 94.44%)@@ develop #2708 diff @@
==========================================
Files 11 11
Lines 3667 3690 +23
Methods 173 174 +1
Messages 0 0
Branches 0 0
==========================================
+ Hits 3459 3486 +27
+ Misses 208 204 -4
Partials 0 0
|
| if ( 'post' === $this->post_type ) { | ||
| $params['sticky'] = array( | ||
| 'description' => __( 'Limit result set to items that are sticky.' ), | ||
| 'type' => 'boolean', |
|
Over to KAdam for review. |
| unset( $args['filter'] ); | ||
| } | ||
|
|
||
| if ( isset( $request['sticky'] ) ) { |
There was a problem hiding this comment.
As noted in slack, sticky=false does not work (it's interpreted as true due to string coercion -- suggest using rest_is_boolean introduced in #2630
| $posts = $response->get_data(); | ||
| $post = $posts[0]; | ||
| $this->assertEquals( $id1, $post['id'] ); | ||
| } |
There was a problem hiding this comment.
Not sure if it's worth it but I think this test (variant of the one above) will demonstrate whether sticky=false in query string will work:
public function test_get_items_not_sticky_query_string_boolean() {
$id1 = $this->post_id;
$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
update_option( 'sticky_posts', array( $id2 ) );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'sticky', 'false' );
$response = $this->server->dispatch( $request );
$this->assertCount( 1, $response->get_data() );
$posts = $response->get_data();
$post = $posts[0];
$this->assertEquals( $id1, $post['id'] );
}There was a problem hiding this comment.
After discussion w/ @BE-Webdesign & @rmccue testing this case independently is probably overkill once the right sanitize callback is put in place
| if ( 'post' === $this->post_type ) { | ||
| $params['sticky'] = array( | ||
| 'description' => __( 'Limit result set to items that are sticky.' ), | ||
| 'type' => 'boolean', |
There was a problem hiding this comment.
Add in the rest_validate_request_arg and rest_sanitize_request_arg for validate and sanitize callbacks and this patch will be da hotness!
There was a problem hiding this comment.
This will automajically be done based off the schema already.
There was a problem hiding this comment.
Actually this is not the schema, sorry my bad!
|
@rmccue @BE-Webdesign @kadamwhite ok I pushed an update to fix the |
|
+1 from me, |
This is an anticipatory change for compatibility with the latest develop branch of the REST API plugin, which added support for a "sticky" param used to either return only, or else no, sticky posts, sepending on what boolean value is passed. See WP-API/WP-API#2708
This allows getting posts that are either "only sticky" for "not sticky"
via
sticky=trueorsticky=false. This can also be used in conjunctionwith
includeandexclude.