Skip to content

Commit

Permalink
Merge pull request #3271 from 10up/fix/3226-meta-query
Browse files Browse the repository at this point in the history
Sanitize meta query
  • Loading branch information
felipeelia authored Feb 8, 2023
2 parents 00f73e4 + f4c28ff commit 2da59bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,7 @@ protected function parse_meta_queries( $args ) {
* @since 1.3
*/
$meta_queries = ( ! empty( $args['meta_query'] ) ) ? $args['meta_query'] : [];
$meta_queries = ( new \WP_Meta_Query() )->sanitize_query( $meta_queries );

/**
* Todo: Support meta_type
Expand Down
42 changes: 42 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3231,6 +3231,48 @@ public function testMetaQueryAdvanced() {
$this->assertEquals( 1, $query->found_posts );
}

/**
* Test the sanitization of an empty meta query
*
* @since 4.5.0
* @group post
*/
public function testMetaQueryEmptySanitization() {
$this->ep_factory->post->create(
array(
'post_content' => 'the post content findme',
'meta_input' => array(
'test_key' => 'value1',
'test_key2' => 'value',
),
)
);
$this->ep_factory->post->create(
array(
'post_content' => 'post content findme',
'meta_input' => array(
'test_key' => 'value',
'test_key2' => 'value2',
'test_key3' => 'value',
),
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();
$args = array(
's' => 'findme',
'meta_query' => array(
0 => array(),
),
);

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );
}

/**
* Test a query that searches and filters by a meta value like the query
*
Expand Down

0 comments on commit 2da59bb

Please sign in to comment.