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

$query->get('tax_query') does not show all elements #3176

Closed
1 task done
kastriotkastrati opened this issue Dec 2, 2022 · 2 comments
Closed
1 task done

$query->get('tax_query') does not show all elements #3176

kastriotkastrati opened this issue Dec 2, 2022 · 2 comments
Labels

Comments

@kastriotkastrati
Copy link

kastriotkastrati commented Dec 2, 2022

Describe your question

Hello, so for some time now I have relied on wp's action pre_get_posts to customize logic before the query is run (I use it for things like: If two filters from the same cateogory are selected, the query between them is 'OR' and the other way around).

Hooking into this action, it will look like this:

add_action(
			'pre_get_posts',
			function ($query) {
				...
				$x = $query->get('tax_query', []);
                                ...

This worked on 3.4.0, but is now not working on 4.4.0.

Looking through changelogs and commits, I can see there's been a bit of a change in relation to how you guys work with tax_query, is there any summation of what has been changed in regards to this functionality, are there any caveats in the code I should know to get this working like it used to on 3.4.0, or do you guys have any suggestions beyond that?.

The result of $query->get('tax_query', []); on 3.4.0 gives me all the filters that have been applied among other things. Something like this:

array (size=4)
  'relation' => string 'AND' (length=3)
  0 => 
    array (size=4)
      'taxonomy' => string 'product_visibility' (length=18)
      'field' => string 'term_taxonomy_id' (length=16)
      'terms' => 
        array (size=2)
          0 => int 7
          1 => int 9
      'operator' => string 'NOT IN' (length=6)
  1 => 
    array (size=4)
      'taxonomy' => string 'pa_size' (length=7)
      'field' => string 'slug' (length=4)
      'terms' => 
        array (size=2)
          0 => int 40
          1 => int 38
      'operator' => string 'and' (length=3)
  2 => 
    array (size=4)
      'taxonomy' => string 'pa_sub_season' (length=13)
      'field' => string 'slug' (length=4)
      'terms' => 
        array (size=1)
          0 => int 1
      'operator' => string 'and' (length=3)

Whereas on 4.4.0, I do not get any of this information anymore, other than the product_visibility information, it's always like this regardless of my search and filtering:

array (size=2)
  'relation' => string 'AND' (length=3)
  0 => 
    array (size=4)
      'taxonomy' => string 'product_visibility' (length=18)
      'field' => string 'term_taxonomy_id' (length=16)
      'terms' => 
        array (size=2)
          0 => int 7
          1 => int 9
      'operator' => string 'NOT IN' (length=6)

Any ideas what change between 3.4.0 and 4.4.0 is the reason here?

Code of Conduct

  • I agree to follow this project's Code of Conduct
@felipeelia
Copy link
Member

Hey @kastriotkastrati, that is a very interesting question. In fact, in ElasticPress 4.4.0 we've changed how we apply facets: filters are not applied to the WP Query anymore but to the ES query directly. That change was made (in #3045 and #3076) so we could apply OR filters between meta queries and tax queries, something not possible if using WP Query alone.

With that said, first of all, make sure you try to achieve the results you want by tweaking the "Match Type" setting in the Facets feature configuration. If that does not fully attend to your needs, you can use the ep_facet_query_filters filter with a priority higher than 10 to change how the taxonomy facet works by default.

To make things easier to understand, here is an outline of how the code flows:

  1. A WP_Query is created
  2. ElasticPress will translate that WP_Query into an Elasticsearch query. All filters are applied in the Post::parse_filters() method.
  3. The Facets feature will add its own filter using the apply_facets_filters method.
  4. That filter is built by calling the ep_facet_query_filters filter, so all facet types (meta and taxonomy at this point) can modify the ES query as needed. You will need to hook into that filter after those facet types already modified the filters to the applied (hence the priority greater than 10)

Hopefully, that helps you to adapt your code! Thanks

@kastriotkastrati
Copy link
Author

Hey @felipeelia. I appreciate your concise reply. The note on the priority of the filter was really really important and helpful. I was messing with multiple filters I found on the source code and in the end the final piece turned out to be the priority.

Thank you:)

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

No branches or pull requests

2 participants