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

WP_Query() with taxonomy query affects get_query_var() when es-wp-query is enabled #79

Open
gudmdharalds opened this issue Apr 24, 2019 · 0 comments

Comments

@gudmdharalds
Copy link
Contributor

gudmdharalds commented Apr 24, 2019

Description

When querying using WP_Query() with Elasticsupport enabled, es-wp-query can cause WP_Query objects to drop taxonomy queries attached. This can cause get_query_var('taxonomy') to return an empty string.

For instance, when the main loop calls WP_Query() with a custom taxonomy, like this:

WP_Query( array(
   ...,
   'vertical' => 'chairs',
   'es' => true,
));

then subsequent calls to get_query_var( 'taxonomy' ) will return an empty string. This will not happen with no es argument passed to WP_Query(), as then get_query_var( 'taxonomy') will return the correct value.

Cause

This problem likely occurs because in the function WP_Query::get_posts() the term and other keys of WP_Query::$query_vars are added after the pre_get_posts action hook is called here -- this action hook is used to take over any calls to WP_Query() or get_posts() and offload the query to Elasticsearch. The term key (and others) are added in WP_Query::get_posts() around here.

The action hook implemented by class-es-wp-query-shoehorn.php (es_wp_query_shoehorn) will call WP_Query::parse_query() here, and because it does not add back in terms and taxonomy information after re-parsing, the terms and taxonomy information will be left out -- this will ultimately impact the following:

if ( ! empty( $this->tax_query->queried_terms ) ) {

found in class-wp-query.php (here) as with Elasticsearch enabled, it is evaluated as false and evaluated as true otherwise. It is after this point term and other values are added to WP_Query::$query_vars.

Solution

The solution appears to add the taxonomy information back in after calling WP_Query::parse_query(), which will ensure the terms being available by get_query_var().

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

No branches or pull requests

1 participant