Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Admin searches in WooCommerce taxonomies return no results #2817

Merged
merged 4 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,8 @@ public function build_meta_query( $meta_queries ) {
$compare = '=';
if ( ! empty( $single_meta_query['compare'] ) ) {
$compare = strtolower( $single_meta_query['compare'] );
} elseif ( ! isset( $single_meta_query['value'] ) ) {
$compare = 'exists';
}

$type = null;
Expand Down
25 changes: 13 additions & 12 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,23 +1280,24 @@ public function format_args( $args, $wp_query ) {
$meta_queries = [];

/**
* Support meta_key
*
* @since 2.1
* Support `meta_key`, `meta_value`, `meta_value_num`, and `meta_compare` query args
*/
if ( ! empty( $args['meta_key'] ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't add meta_key if meta_value is not set.

if ( ! empty( $args['meta_value'] ) ) {
$meta_value = $args['meta_value'];
} elseif ( ! empty( $args['meta_value_num'] ) ) {
$meta_value = $args['meta_value_num'];
$meta_query_array = [
'key' => $args['meta_key'],
];

if ( isset( $args['meta_value'] ) && '' !== $args['meta_value'] ) {
$meta_query_array['value'] = $args['meta_value'];
} elseif ( isset( $args['meta_value_num'] ) && '' !== $args['meta_value_num'] ) {
$meta_query_array['value'] = $args['meta_value_num'];
}

if ( ! empty( $meta_value ) ) {
$meta_queries[] = array(
'key' => $args['meta_key'],
'value' => $meta_value,
);
if ( isset( $args['meta_compare'] ) ) {
$meta_query_array['compare'] = $args['meta_compare'];
}

$meta_queries[] = $meta_query_array;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Term/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public function format_args( $query_vars ) {
'key' => $query_vars['meta_key'],
];

if ( isset( $query_vars['meta_value'] ) ) {
if ( isset( $query_vars['meta_value'] ) && '' !== $query_vars['meta_value'] ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to pass meta_value to ES if its value is not empty

$meta_query_array['value'] = $query_vars['meta_value'];
}

Expand Down
64 changes: 59 additions & 5 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5636,7 +5636,7 @@ public function testFormatArgsRootLevelTaxonomies() {
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5649,7 +5649,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post1 ], $query->posts );
$this->assertEquals( 1, $query->post_count );
$this->assertEquals( 1, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5662,7 +5662,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post1, $post2 ], $query->posts );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5675,7 +5675,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post2 ], $query->posts );
$this->assertEquals( 1, $query->post_count );
$this->assertEquals( 1, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5687,7 +5687,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post1, $post2, $post4 ], $query->posts );
$this->assertEquals( 3, $query->post_count );
$this->assertEquals( 3, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand Down Expand Up @@ -6813,4 +6813,58 @@ public function testPostEditedTerm() {
$this->assertEquals( 'different-tag-slug', $document['terms']['post_tag'][0]['slug'] );
$this->assertEquals( 'Different Tag Name', $document['terms']['post_tag'][0]['name'] );
}

/**
* Tests post without meta value.
*
* @return void
*/
public function testMetaWithoutValue() {

Functions\create_and_sync_post( array(), array( 'test_key' => '' ) );
Functions\create_and_sync_post( array(), array( 'test_key' => '' ) );
Functions\create_and_sync_post();

$expected_result = '2';

ElasticPress\Elasticsearch::factory()->refresh_indices();

// Make sure WordPress returns only 2 posts.
$args = array(
'meta_query' => array(
array(
'key' => 'test_key',
),
),
);
$query = new \WP_Query( $args );

$this->assertEquals( $expected_result, $query->post_count );
$this->assertNull( $query->elasticsearch_success );

// Make sure ElasticPress returns only 2 posts when meta query is set
$args = array(
'ep_integrate' => true,
'meta_query' => array(
array(
'key' => 'test_key',
),
),
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( $expected_result, $query->post_count );

// Make sure ElasticPress returns only 2 posts when meta key is set
$args = array(
'ep_integrate' => true,
'meta_key' => 'test_key',
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( $expected_result, $query->post_count );

}
}
48 changes: 48 additions & 0 deletions tests/php/indexables/TestTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1580,4 +1580,52 @@ public function testIntegrateSearchQueries() {

$this->assertTrue( $this->get_feature()->integrate_search_queries( false, $query ) );
}

/**
* Tests terms without meta value.
*
* @return void
*/
public function testMetaWithoutValue() {

$term_id = Functions\create_and_sync_term( 'term-name', 'term name', '', 'category' );
update_term_meta( $term_id, 'test_key', 'value' );
ElasticPress\Indexables::factory()->get( 'term' )->index( $term_id, true );

$term_id = Functions\create_and_sync_term( 'term-name-2', 'term name 2', '', 'category' );
update_term_meta( $term_id, 'test_key', 'value' );
ElasticPress\Indexables::factory()->get( 'term' )->index( $term_id, true );

Functions\create_and_sync_term( 'term-name-3', 'term name 3', '', 'category' );

ElasticPress\Elasticsearch::factory()->refresh_indices();

// Make sure WP_Term_Query returns only taxonomies for whom meta exists.
$args = array(
'taxonomy' => 'category',
'meta_key' => 'test_key',
'ep_integrate' => true,
'hide_empty' => false,

);
$query = new \WP_Term_Query( $args );
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, count( $query->terms ) );

// Make sure get_terms returns only taxonomies for whom meta exists.
$args = array(
'taxonomy' => 'category',
'hide_empty' => false,
'ep_integrate' => true,
'meta_query' => array(
array(
'key' => 'test_key',
),
),
);
$terms = get_terms( $args );
$this->assertTrue( $terms[0]->elasticsearch );
$this->assertEquals( 2, count( $terms ) );
}

}