Skip to content

Commit

Permalink
Merge pull request WP-API#2756 from WP-API/support_taxonomy_term_excl…
Browse files Browse the repository at this point in the history
…usion

Add `?{taxonomy}_exclude=` query parameter
  • Loading branch information
joehoyle committed Oct 6, 2016
2 parents 7dcbf48 + 2f96ca7 commit 50b1f24
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/endpoints/class-wp-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function get_items( $request ) {
$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
foreach ( $taxonomies as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$tax_exclude = $base . '_exclude';

if ( ! empty( $request[ $base ] ) ) {
$query_args['tax_query'][] = array(
Expand All @@ -180,6 +181,16 @@ public function get_items( $request ) {
'include_children' => false,
);
}

if ( ! empty( $request[ $tax_exclude ] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => $taxonomy->name,
'field' => 'term_id',
'terms' => $request[ $tax_exclude ],
'include_children' => false,
'operator' => 'NOT IN',
);
}
}

$posts_query = new WP_Query();
Expand Down
45 changes: 44 additions & 1 deletion tests/test-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,28 @@ public function test_get_items_tags_query() {
$request->set_param( 'tags', array( $tag['term_id'] ) );

$response = $this->server->dispatch( $request );
$this->assertCount( 1, $response->get_data() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $id1, $data[0]['id'] );
}

public function test_get_items_tags_exclude_query() {
$id1 = $this->post_id;
$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
$id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
$tag = wp_insert_term( 'My Tag', 'post_tag' );

wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'tags_exclude', array( $tag['term_id'] ) );

$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertCount( 3, $data );
$this->assertEquals( $id4, $data[0]['id'] );
$this->assertEquals( $id3, $data[1]['id'] );
$this->assertEquals( $id2, $data[2]['id'] );
}

public function test_get_items_tags_and_categories_query() {
Expand All @@ -358,6 +379,28 @@ public function test_get_items_tags_and_categories_query() {
$this->assertCount( 1, $response->get_data() );
}

public function test_get_items_tags_and_categories_exclude_query() {
$id1 = $this->post_id;
$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
$id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
$tag = wp_insert_term( 'My Tag', 'post_tag' );
$category = wp_insert_term( 'My Category', 'category' );

wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' );
wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' );

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'tags', array( $tag['term_id'] ) );
$request->set_param( 'categories_exclude', array( $category['term_id'] ) );

$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $id2, $data[0]['id'] );
}

public function test_get_items_sticky_query() {
$id1 = $this->post_id;
$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
Expand Down

0 comments on commit 50b1f24

Please sign in to comment.