Skip to content

Commit 907d956

Browse files
authored
Merge pull request #4168 from 10up/fix/4164
Fix: Date Query handles array for Compare IN and NOT IN
2 parents bc36961 + 08a4c15 commit 907d956

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

includes/classes/Indexable/Post/DateQuery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ protected function get_es_filter_for_clause( $query ) {
263263
} elseif ( '!=' === $compare ) {
264264
$date_terms['must_not'][]['term'][ "date_terms.{$param}" ] = $value;
265265
} elseif ( 'IN' === $compare ) {
266-
foreach ( $value as $in_value ) {
266+
foreach ( (array) $value as $in_value ) {
267267
$date_terms['should'][]['term'][ "date_terms.{$param}" ] = $in_value;
268268
}
269269
} elseif ( 'NOT IN' === $compare ) {
270-
foreach ( $value as $in_value ) {
270+
foreach ( (array) $value as $in_value ) {
271271
$date_terms['must_not'][]['term'][ "date_terms.{$param}" ] = $in_value;
272272
}
273273
} elseif ( 'BETWEEN' === $compare ) {

tests/php/indexables/TestPost.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5968,6 +5968,124 @@ public function testDateQueryWeekdayRange() {
59685968
$this->assertEquals( 9, $query->found_posts );
59695969
}
59705970

5971+
/**
5972+
* Test a date query with IN comparison
5973+
*
5974+
* @group post
5975+
*/
5976+
public function test_date_query_compare_in() {
5977+
$this->ep_factory->post->create(
5978+
[
5979+
'post_date' => '2023-01-01',
5980+
]
5981+
);
5982+
5983+
$this->ep_factory->post->create(
5984+
[
5985+
'post_date' => '2024-01-01',
5986+
]
5987+
);
5988+
5989+
$this->ep_factory->post->create(
5990+
[
5991+
'post_date' => '2025-01-01',
5992+
]
5993+
);
5994+
5995+
ElasticPress\Elasticsearch::factory()->refresh_indices();
5996+
5997+
$query = new \WP_Query(
5998+
[
5999+
'ep_integrate' => true,
6000+
'date_query' => [
6001+
[
6002+
'year' => 2023,
6003+
'compare' => 'IN',
6004+
],
6005+
],
6006+
]
6007+
);
6008+
6009+
$this->assertTrue( $query->elasticsearch_success );
6010+
$this->assertEquals( 1, $query->post_count );
6011+
$this->assertEquals( 1, $query->found_posts );
6012+
6013+
$query = new \WP_Query(
6014+
[
6015+
'ep_integrate' => true,
6016+
'date_query' => [
6017+
[
6018+
'year' => [ 2023, 2025 ],
6019+
'compare' => 'IN',
6020+
],
6021+
],
6022+
]
6023+
);
6024+
6025+
$this->assertTrue( $query->elasticsearch_success );
6026+
$this->assertEquals( 2, $query->post_count );
6027+
$this->assertEquals( 2, $query->found_posts );
6028+
}
6029+
6030+
/**
6031+
* Test a date query with NOT IN comparison
6032+
*
6033+
* @group post
6034+
*/
6035+
public function test_date_query_compare_not_in() {
6036+
$this->ep_factory->post->create(
6037+
[
6038+
'post_date' => '2023-01-01',
6039+
]
6040+
);
6041+
6042+
$this->ep_factory->post->create(
6043+
[
6044+
'post_date' => '2024-01-01',
6045+
]
6046+
);
6047+
6048+
$this->ep_factory->post->create(
6049+
[
6050+
'post_date' => '2025-01-01',
6051+
]
6052+
);
6053+
6054+
ElasticPress\Elasticsearch::factory()->refresh_indices();
6055+
6056+
$query = new \WP_Query(
6057+
[
6058+
'ep_integrate' => true,
6059+
'date_query' => [
6060+
[
6061+
'year' => 2024,
6062+
'compare' => 'NOT IN',
6063+
],
6064+
],
6065+
]
6066+
);
6067+
6068+
$this->assertTrue( $query->elasticsearch_success );
6069+
$this->assertEquals( 2, $query->post_count );
6070+
$this->assertEquals( 2, $query->found_posts );
6071+
6072+
$query = new \WP_Query(
6073+
[
6074+
'ep_integrate' => true,
6075+
'date_query' => [
6076+
[
6077+
'year' => [ 2023, 2025 ],
6078+
'compare' => 'NOT IN',
6079+
],
6080+
],
6081+
]
6082+
);
6083+
6084+
$this->assertTrue( $query->elasticsearch_success );
6085+
$this->assertEquals( 1, $query->post_count );
6086+
$this->assertEquals( 1, $query->found_posts );
6087+
}
6088+
59716089
/**
59726090
* Check if elasticpress_enabled() properly handles an object without the is_search() method.
59736091
*

0 commit comments

Comments
 (0)