Skip to content

Commit 3317d96

Browse files
authored
Merge pull request #84 from lipemat/feature/is_main_query
Feature/is main query
2 parents 7cf8e3e + 74bdfd6 commit 3317d96

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

class-es-wp-query-shoehorn.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function es_wp_query_arg( $vars ) {
2424
* If a WP_Query object has `'es' => true`, use Elasticsearch to run the meat of the query.
2525
* This is fires on the "pre_get_posts" action.
2626
*
27-
* @param object $query WP_Query object.
27+
* @param WP_Query $query - Current full WP_Query object.
2828
* @return void
2929
*/
3030
function es_wp_query_shoehorn( &$query ) {
@@ -78,6 +78,7 @@ function es_wp_query_shoehorn( &$query ) {
7878
*/
7979
$es_query_args = $query->query;
8080
$es_query_args['fields'] = 'ids';
81+
$es_query_args['es_is_main_query'] = $query->is_main_query();
8182
$es_query = new ES_WP_Query( $es_query_args );
8283

8384
// Make the post query use the post IDs from the ES results instead.
@@ -90,7 +91,7 @@ function es_wp_query_shoehorn( &$query ) {
9091
'fields' => $query->get( 'fields' ),
9192
'orderby' => 'post__in',
9293
'order' => 'ASC',
93-
)
94+
)
9495
);
9596

9697
// Reinsert all the conditionals from the original query.

class-es-wp-query-wrapper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class ES_WP_Query_Wrapper extends WP_Query {
3434
*/
3535
public $es_response;
3636

37+
3738
/**
3839
* Construct for querying Elasticsearch. Must be implemented in child classes.
3940
*
@@ -43,6 +44,19 @@ abstract class ES_WP_Query_Wrapper extends WP_Query {
4344
*/
4445
abstract protected function query_es( $es_args );
4546

47+
/**
48+
* Override default WP_Query->is_main_query() to support
49+
* this conditional when the main query has been overridden
50+
* by this class.
51+
*
52+
* @fixes #38
53+
*
54+
* @return bool
55+
*/
56+
public function is_main_query() {
57+
return $this->get( 'es_is_main_query', false );
58+
}
59+
4660
/**
4761
* Maps a field to its Elasticsearch context.
4862
*

0 commit comments

Comments
 (0)