From 3adfda727ce38265357df61287f698af87357364 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Tue, 4 May 2021 13:00:52 -0500 Subject: [PATCH 1/5] Add post__name_in support --- includes/classes/Indexable/Post/Post.php | 15 ++++++++++++ tests/php/indexables/TestPost.php | 29 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 21b1fef69c..ef1f7bb8d1 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -863,6 +863,21 @@ function( $tax_query ) use ( $args ) { $use_filters = true; } + /** + * 'post_name__in' arg support. + * + * @since 3.6.0 + */ + if ( ! empty( $args['post_name__in'] ) ) { + $filter['bool']['must'][]['bool']['must'] = array( + 'term' => array( + 'post_name.raw' => $args['post_name__in'] , + ), + ); + + $use_filters = true; + } + /** * 'post__not_in' arg support. * diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index b4da6d9877..ca78f0d0fd 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -3637,6 +3637,35 @@ public function testPostParentQuery() { $this->assertEquals( 1, $query->found_posts ); } + /** + * Test a post_name__in query + * + * @group post + * @since 3.6.0 + */ + public function testPostNameInQuery() { + Functions\create_and_sync_post( + array( + 'post_content' => 'findme name in test 1', + 'post_name' => 'findme-name-in', + ) + ); + Functions\create_and_sync_post( array( 'post_content' => 'findme name in test 2' ) ); + Functions\create_and_sync_post( array( 'post_content' => 'findme name in test 3' ) ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => 'findme name in', + 'post_name__in' => 'findme-name-in', + ); + + $query = new \WP_Query( $args ); + + $this->assertEquals( 1, $query->post_count ); + $this->assertEquals( 1, $query->found_posts ); + } + /** * Test Tax Query NOT IN operator * From 6d55f18b285bc4bb3e675f598b50e3378dd6829a Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Tue, 4 May 2021 18:54:23 -0500 Subject: [PATCH 2/5] Fix linting issues --- includes/classes/Indexable/Post/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index ef1f7bb8d1..c3a977b9f8 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -871,7 +871,7 @@ function( $tax_query ) use ( $args ) { if ( ! empty( $args['post_name__in'] ) ) { $filter['bool']['must'][]['bool']['must'] = array( 'term' => array( - 'post_name.raw' => $args['post_name__in'] , + 'post_name.raw' => $args['post_name__in'], ), ); From 3c589497e124701a4407680c32233992472fc621 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Wed, 12 May 2021 19:44:56 -0500 Subject: [PATCH 3/5] Wrap arg with array_values --- includes/classes/Indexable/Post/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 888457bd7d..77821e11e7 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -878,7 +878,7 @@ function( $tax_query ) use ( $args ) { if ( ! empty( $args['post_name__in'] ) ) { $filter['bool']['must'][]['bool']['must'] = array( 'term' => array( - 'post_name.raw' => $args['post_name__in'], + 'post_name.raw' => array_values( (array) $args['post_name__in'] ), ), ); From 0e42db1060278896305f65365e6cb065506df0ef Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Thu, 13 May 2021 17:10:47 -0500 Subject: [PATCH 4/5] Change term query for terms --- includes/classes/Indexable/Post/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 77821e11e7..8d7f66035d 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -877,7 +877,7 @@ function( $tax_query ) use ( $args ) { */ if ( ! empty( $args['post_name__in'] ) ) { $filter['bool']['must'][]['bool']['must'] = array( - 'term' => array( + 'terms' => array( 'post_name.raw' => array_values( (array) $args['post_name__in'] ), ), ); From 04e136aaf668eaaa2ac3a331325ab1c1f4b4d782 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Thu, 20 May 2021 16:09:41 -0500 Subject: [PATCH 5/5] Add tests for array post_name__in scenario --- tests/php/indexables/TestPost.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index f1f4bed9bb..17a0e812a2 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -3702,13 +3702,20 @@ public function testPostNameInQuery() { $args = array( 's' => 'findme name in', - 'post_name__in' => 'findme-name-in', ); + $args['post_name__in'] = 'findme-name-in'; + $query = new \WP_Query( $args ); + $args['post_name__in'] = array( 'findme-name-in' ); + + $query2 = new \WP_Query( $args ); + $this->assertEquals( 1, $query->post_count ); $this->assertEquals( 1, $query->found_posts ); + $this->assertEquals( 1, $query2->post_count ); + $this->assertEquals( 1, $query2->found_posts ); } /**