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

BUG: WP_Term_Query always fallback to MySQL when results are empty #2367

Closed
felipeelia opened this issue Sep 24, 2021 · 0 comments · Fixed by #2420
Closed

BUG: WP_Term_Query always fallback to MySQL when results are empty #2367

felipeelia opened this issue Sep 24, 2021 · 0 comments · Fixed by #2420

Comments

@felipeelia
Copy link
Member

felipeelia commented Sep 24, 2021

Describe the bug

ElasticPress\Indexable\Term\QueryIntegration::maybe_filter_query (here) should short-circuit WP_Term_Query::get_terms() but when it doesn't find any term it returns null (as $new_terms is not defined) and WP keeps its process.

Steps to Reproduce
Execute the following code:

// Add the term but does not index to ES
add_filter( 'ep_term_sync_kill', '__return_true' );
wp_insert_term( 'mytestterm', 'category' );
remove_filter( 'ep_term_sync_kill', '__return_true' );

// Search for the term
$terms = new WP_Term_Query(
	array(
		'taxonomy' => 'category',
		'search' => 'mytestterm',
		'hide_empty' => false,
		'ep_integrate' => true,
	)
);
echo '<pre>';
print_r( $terms );
echo '</pre>';

The result will contain the term (without the elasticsearch attribute) but $terms will have elasticsearch_success set to 1 and found_terms as 0, as that is set by EP, that didn't find anything on ES.

Expected behavior
If nothing was found on ES but the request was successful, we should avoid fallback to MySQL.

Additional context

Setting $new_terms = []; before switch ( $fields ) { ensures we'll return at least an empty array rather than null. Although the solution is easy, we'll have to fix a lot of our unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants