alleyinteractive / alleyinteractive/es-wp-query
WP_Query() with taxonomy query affects get_query_var() when es-wp-query is enabled
- 主要語言
- PHP
- 星號
- 125
- 分支
- 28
- PR 合併指標
- 30 天內沒有已合併 PR
描述
# 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](https://github.com/WordPress/WordPress/blob/5.1-branch/wp-includes/class-wp-query.php#L1736) -- 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](https://github.com/WordPress/WordPress/blob/5.1-branch/wp-includes/class-wp-query.php#L2134).
The action hook implemented by `class-es-wp-query-shoehorn.php` (`es_wp_query_shoehorn`) will call `WP_Query::parse_query()` [here](https://github.com/alleyinteractive/es-wp-query/blob/master/class-es-wp-query-shoehorn.php#L84), 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](https://github.com/WordPress/WordPress/blob/5.1-branch/wp-includes/class-wp-query.php#L2122)) 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()`.
貢獻指南
評估
這個 Issue 還沒有評估資料。