add support to NOT LIKE operator in tax_query #266

This commit is contained in:
Leo Germani 2019-07-30 15:35:23 -03:00
parent 594c613f9e
commit 2fd4bb0ad6
2 changed files with 85 additions and 0 deletions

View File

@ -172,6 +172,23 @@ class REST_Controller extends \WP_REST_Controller {
'terms' => $terms,
];
} elseif ( isset($tax_query['operator']) && $tax_query['operator'] == 'NOT LIKE' &&
isset($tax_query['terms']) && is_string($tax_query['terms']) ) {
$terms = get_terms([
'taxonomy' => $tax_query['taxonomy'],
'fields' => 'ids',
'search' => $tax_query['terms']
]);
if ($terms) {
$new_tax_query[] = [
'taxonomy' => $tax_query['taxonomy'],
'terms' => $terms,
'operator' => 'NOT IN'
];
}
} else {
$new_tax_query[] = $tax_query;
}

View File

@ -866,6 +866,74 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$ids = array_map(function($e) { return $e['id']; }, $data);
$this->assertContains($item2->get_id(), $ids);
####################################################
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'taxquery' => [
[
'taxonomy' => $taxonomy->get_db_identifier(),
'operator' => 'NOT LIKE',
'terms' => 'mellon'
]
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(1, count($data));
$ids = array_map(function($e) { return $e['id']; }, $data);
$this->assertContains($item3->get_id(), $ids);
####################################################
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'taxquery' => [
[
'taxonomy' => $taxonomy->get_db_identifier(),
'operator' => 'NOT LIKE',
'terms' => '__does_not_exists'
]
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(3, count($data));
####################################################
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'taxquery' => [
[
'taxonomy' => $taxonomy->get_db_identifier(),
'operator' => 'LIKE',
'terms' => '__does_not_exists'
]
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(0, count($data));
}
}