diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 13d8f9690c..b17dcd8ae9 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1996,7 +1996,19 @@ protected function parse_date( $args ) { * @return array */ protected function parse_meta_queries( $args ) { - $meta_queries = []; + /** + * 'meta_query' arg support. + * + * Relation supports 'AND' and 'OR'. 'AND' is the default. For each individual query, the + * following 'compare' values are supported: =, !=, EXISTS, NOT EXISTS. '=' is the default. + * + * @since 1.3 + */ + $meta_queries = ( ! empty( $args['meta_query'] ) ) ? $args['meta_query'] : []; + + /** + * Todo: Support meta_type + */ /** * Support `meta_key`, `meta_value`, `meta_value_num`, and `meta_compare` query args @@ -2016,32 +2028,18 @@ protected function parse_meta_queries( $args ) { $meta_query_array['compare'] = $args['meta_compare']; } - $meta_queries[] = $meta_query_array; - } - - /** - * Todo: Support meta_type - */ - - /** - * 'meta_query' arg support. - * - * Relation supports 'AND' and 'OR'. 'AND' is the default. For each individual query, the - * following 'compare' values are supported: =, !=, EXISTS, NOT EXISTS. '=' is the default. - * - * @since 1.3 - */ - if ( ! empty( $args['meta_query'] ) ) { - $meta_queries = array_merge( $meta_queries, $args['meta_query'] ); + if ( ! empty( $meta_queries ) ) { + $meta_queries = [ + 'relation' => 'AND', + $meta_query_array, + $meta_queries, + ]; + } else { + $meta_queries = [ $meta_query_array ]; + } } if ( ! empty( $meta_queries ) ) { - - $relation = 'must'; - if ( ! empty( $args['meta_query'] ) && ! empty( $args['meta_query']['relation'] ) && 'or' === strtolower( $args['meta_query']['relation'] ) ) { - $relation = 'should'; - } - // get meta query filter $meta_filter = $this->build_meta_query( $meta_queries ); diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index ffc12b9daa..b195c3bdd6 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -3066,6 +3066,75 @@ public function testMetaQueryOrRelation() { $this->assertEquals( 2, $query->found_posts ); } + /** + * Test an advanced meta filter query with or relation while sorting by Meta key + * + * @since 4.4.0 + * @group post + */ + public function testMetaQueryOrRelationWithSort() { + $this->ep_factory->post->create( + array( + 'post_content' => 'the post content findme', + 'meta_input' => array( 'test_key' => date('Ymd') - 5 ), + ), + ); + $this->ep_factory->post->create( + array( + 'post_content' => 'the post content findme', + 'meta_input' => array( + 'test_key' => date('Ymd') + 5, + 'test_key2' => date('Ymd') + 6, + ), + ), + ); + $this->ep_factory->post->create( + array( + 'post_content' => 'the post content findme', + 'meta_input' => array( + 'test_key' => date('Ymd') + 5, + 'test_key2' => date('Ymd') + 6, + ), + ), + ); + + $post = new \ElasticPress\Indexable\Post\Post(); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + $args = array( + 'ep_integrate' => true, + 'meta_key' => 'test_key', + 'meta_query' => array( + 'relation' => 'or', + array( + 'key' => 'test_key', + 'value' => date('Ymd'), + 'compare' => '<=', + 'type' => 'NUMERIC', + ), + array( + 'key' => 'test_key2', + 'value' => date('Ymd'), + 'compare' => '>=', + 'type' => 'NUMERIC', + ), + ), + 'orderby' => 'meta_value_num', + 'order' => 'ASC', + ); + + $query = new \WP_Query( $args ); + $args = $post->format_args($args, new \WP_Query() ); + + $outer_must = $args['post_filter']['bool']['must'][0]['bool']['must']; + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 3, $query->post_count ); + $this->assertEquals( 3, $query->found_posts ); + $this->assertSame( 'meta.test_key', $outer_must[0]['exists']['field'] ); + $this->assertArrayHasKey( 'meta.test_key.long', $outer_must[1]['bool']['should']['bool']['should'][0]['bool']['must'][0]['range'] ); + $this->assertArrayHasKey( 'meta.test_key2.long', $outer_must[1]['bool']['should']['bool']['should'][1]['bool']['must'][0]['range'] ); + } + /** * Test an advanced meta filter query *