-
Notifications
You must be signed in to change notification settings - Fork 29
Description
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().