From a7515214dd1e73c6582ccaefbe3a4729310028f1 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 12 Jan 2016 10:40:42 -0800 Subject: [PATCH 1/3] Add `exclude` param to `GET /wp/v2/posts` --- lib/endpoints/class-wp-rest-posts-controller.php | 9 ++++++++- tests/test-rest-posts-controller.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/endpoints/class-wp-rest-posts-controller.php b/lib/endpoints/class-wp-rest-posts-controller.php index 780fe6e364..0cbefdb601 100755 --- a/lib/endpoints/class-wp-rest-posts-controller.php +++ b/lib/endpoints/class-wp-rest-posts-controller.php @@ -74,6 +74,7 @@ public function get_items( $request ) { $args['orderby'] = $request['orderby']; $args['paged'] = $request['page']; $args['post__in'] = $request['include']; + $args['post__not_in'] = $request['exclude']; $args['posts_per_page'] = $request['per_page']; $args['post_parent'] = $request['parent']; $args['post_status'] = $request['status']; @@ -585,7 +586,7 @@ protected function get_allowed_query_vars() { $valid_vars = array_merge( $valid_vars, $private ); } // Define our own in addition to WP's normal vars. - $rest_valid = array( 'post__in', 'posts_per_page', 'ignore_sticky_posts', 'post_parent' ); + $rest_valid = array( 'post__in', 'post__not_in', 'posts_per_page', 'ignore_sticky_posts', 'post_parent' ); $valid_vars = array_merge( $valid_vars, $rest_valid ); /** @@ -1576,6 +1577,12 @@ public function get_collection_params() { 'sanitize_callback' => 'absint', ); } + $params['exclude'] = array( + 'description' => __( 'Ensure result set excludes specific ids.' ), + 'type' => 'array', + 'default' => array(), + 'sanitize_callback' => 'wp_parse_id_list', + ); $params['include'] = array( 'description' => __( 'Limit result set to specific ids.' ), 'type' => 'array', diff --git a/tests/test-rest-posts-controller.php b/tests/test-rest-posts-controller.php index a9c26a38b9..ec6b40134f 100644 --- a/tests/test-rest-posts-controller.php +++ b/tests/test-rest-posts-controller.php @@ -126,6 +126,21 @@ public function test_get_items_include_query() { $this->assertEquals( $id1, $data[0]['id'] ); } + public function test_get_items_exclude_query() { + $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) ); + $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ) ) ); + $request->set_param( 'exclude', array( $id2 ) ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ) ) ); + $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ) ) ); + } + public function test_get_items_search_query() { for ( $i = 0; $i < 5; $i++ ) { $this->factory->post->create( array( 'post_status' => 'publish' ) ); From f02de72c6075ddeed7810d9305abb5c634c1c1cf Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 12 Jan 2016 11:02:41 -0800 Subject: [PATCH 2/3] Update tests for new `exclude` param --- tests/test-rest-posts-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test-rest-posts-controller.php b/tests/test-rest-posts-controller.php index ec6b40134f..6b56e66937 100644 --- a/tests/test-rest-posts-controller.php +++ b/tests/test-rest-posts-controller.php @@ -59,6 +59,7 @@ public function test_registered_query_params() { $this->assertEquals( array( 'author', 'context', + 'exclude', 'filter', 'include', 'order', From 2ca2f414fcecf0c5bb8085cfab931c59f9e59303 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 12 Jan 2016 11:31:19 -0800 Subject: [PATCH 3/3] I was going crazy... but this needs to be updated twice --- tests/test-rest-pages-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test-rest-pages-controller.php b/tests/test-rest-pages-controller.php index f0a84292ef..41774d4f99 100644 --- a/tests/test-rest-pages-controller.php +++ b/tests/test-rest-pages-controller.php @@ -52,6 +52,7 @@ public function test_registered_query_params() { $this->assertEquals( array( 'author', 'context', + 'exclude', 'filter', 'include', 'order',