Skip to content

Commit

Permalink
Merge pull request #9 from Automattic/vip/fix_is_main_query
Browse files Browse the repository at this point in the history
Fix is_main_query() not registering when hooked onto pre_get_posts
  • Loading branch information
nickdaugherty authored Oct 5, 2020
2 parents 7f661cf + dc95da4 commit a809688
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion class-es-wp-query-shoehorn.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function es_wp_query_arg( $vars ) {
* If a WP_Query object has `'es' => true`, use Elasticsearch to run the meat of the query.
* This is fires on the "pre_get_posts" action.
*
* @param object $query WP_Query object.
* @param WP_Query $query Current full WP_Query object.
* @return void
*/
function es_wp_query_shoehorn( &$query ) {
Expand Down Expand Up @@ -78,6 +78,7 @@ function es_wp_query_shoehorn( &$query ) {
*/
$es_query_args = $query->query;
$es_query_args['fields'] = 'ids';
$es_query_args['es_is_main_query'] = $query->is_main_query();
$es_query = new ES_WP_Query( $es_query_args );

// Make the post query use the post IDs from the ES results instead.
Expand Down
13 changes: 13 additions & 0 deletions class-es-wp-query-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ abstract class ES_WP_Query_Wrapper extends WP_Query {
*/
abstract protected function query_es( $es_args );

/**
* Override default WP_Query->is_main_query() to support
* this conditional when the main query has been overridden
* by this class.
*
* @fixes #38
*
* @return bool
*/
public function is_main_query() {
return $this->get( 'es_is_main_query', false );
}

/**
* Maps a field to its Elasticsearch context.
*
Expand Down

0 comments on commit a809688

Please sign in to comment.