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

Conversation

burhandodhy
Copy link
Contributor

@burhandodhy burhandodhy commented Jun 2, 2022

Description of the Change

This PR fixes the issue where the EP returns incorrect result when the meta_value key is not set in WP_Query and WP_Term_Query.

When the meta_value is not set, WordPress runs the exact SQL query that it runs in case of the meta_compare => 'Exists' and when the meta_value is empty, WordPress only returns the posts where the meta value is empty. This statement is only true for the WP Query and not for the Term Query

For the Term Query, WordPress runs the same query either the meta_value is set to empty or its not set. The query in both cases is exact what we have in the case of Exists. To tackle this case, I added another check that check if the meta_value is not empty indexable Term class

Closes #2791

Possible Drawbacks

I didn't find any negative impact. I tried to test all the cases and its working fine.

Verification Process

  1. Import WooCommerce Dummy data.
  2. Run the below code first and then uncomment ep_integrate line and run it again. In both cases result should be same
	$args = array(
		'taxonomy'   => 'product_cat',
		// 'ep_integrate' => 1,
		'hide_empty' => false,
		'meta_key' => 'order',
		'meta_value' => 0
	);
	var_dump( get_terms( $args ) );
  1. To run the test against WP_Query. Run the below code twice. First without EP and second time with EP integration. In both case the result should be same.
	$args = array(
		'post_type'  => 'post',
		// 'ep_integrate' => 1,
		'posts_per_page' => -1,
		'meta_query' => array(
			array(
	 			'key' => 'invalid',
			),
		),
	);
	$query = new WP_Query( $args );

	var_dump( $query->post_count );

  1. If you test the both snippet with develop branch, you will the result EP result will not be same what WordPress returns.
  2. To test against admin search in WooCommerce taxonomies.
    • Go to WordPress admin dashboard -> Products -> Categories
    • Search Clothing
    • It should return only returns Clothing category.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Fixed - Meta queries with 'exists' as compare operator and empty meta values handling

Credits

Props @burhandodhy

@burhandodhy burhandodhy self-assigned this Jun 3, 2022
* Support meta_key
*
* @since 2.1
* Support `meta_key`, `meta_value`, 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.

@@ -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

@felipeelia felipeelia added this to the 4.3.0 milestone Jun 3, 2022
@burhandodhy burhandodhy marked this pull request as ready for review June 3, 2022 12:20
@felipeelia felipeelia merged commit b05b3d0 into develop Jun 20, 2022
@felipeelia felipeelia deleted the fix/2791 branch June 20, 2022 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Admin searches in WooCommerce taxonomies return no results
2 participants