Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Sep 24, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion 9 lib/endpoints/class-wp-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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 );

/**
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions 1 tests/test-rest-pages-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function test_registered_query_params() {
$this->assertEquals( array(
'author',
'context',
'exclude',
'filter',
'include',
'order',
Expand Down
16 changes: 16 additions & 0 deletions 16 tests/test-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function test_registered_query_params() {
$this->assertEquals( array(
'author',
'context',
'exclude',
'filter',
'include',
'order',
Expand Down Expand Up @@ -126,6 +127,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' ) );
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.