diff --git a/includes/classes/Indexable/Term/QueryIntegration.php b/includes/classes/Indexable/Term/QueryIntegration.php index b061566666..9dfbe8b7c8 100644 --- a/includes/classes/Indexable/Term/QueryIntegration.php +++ b/includes/classes/Indexable/Term/QueryIntegration.php @@ -79,6 +79,10 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) { return $results; } + if ( ! $this->is_searchable( $query ) ) { + return $results; + } + $new_terms = apply_filters( 'ep_wp_query_cached_terms', null, $query ); if ( null === $new_terms ) { @@ -333,4 +337,21 @@ protected function format_hits_as_names( $terms, $new_terms ) { return $new_terms; } + /** + * Determine whether ES should be used for the query if all taxonomies are indexable. + * + * @param \WP_Term_Query $query The WP_Term_Query object. + * @return boolean + */ + protected function is_searchable( $query ) { + + $taxonomies = $query->query_vars['taxonomy']; + if ( ! $taxonomies ) { + return true; + } + + $indexable_taxonomies = Indexables::factory()->get( 'term' )->get_indexable_taxonomies(); + return empty( array_diff( $taxonomies, $indexable_taxonomies ) ); + } + } diff --git a/tests/php/indexables/TestTerm.php b/tests/php/indexables/TestTerm.php index d74fde16a9..07636ab3a3 100644 --- a/tests/php/indexables/TestTerm.php +++ b/tests/php/indexables/TestTerm.php @@ -1628,4 +1628,46 @@ public function testMetaWithoutValue() { $this->assertEquals( 2, count( $terms ) ); } + /** + * Test term query for non-public taxonomies. + * + * @return void + */ + public function testQueryForNonPublicTaxonomies() { + register_taxonomy( + 'wptests_tax', + 'post', + array( + 'public' => false, + ) + ); + + Functions\create_and_sync_term( 'term-name-1', 'term name 1', '', 'wptests_tax' ); + Functions\create_and_sync_term( 'term-name-2', 'term name 2', '', 'wptests_tax' ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $term_query = new \WP_Term_Query( + [ + 'taxonomy' => 'wptests_tax', + 'ep_integrate' => true, + 'hide_empty' => false, + ] + ); + $this->assertObjectNotHasAttribute( 'elasticsearch_success', $term_query ); + $this->assertEquals( 2, count( $term_query->terms ) ); + + Functions\create_and_sync_term( 'tag-name-1', 'tag name 1', '', 'post_tag' ); + Functions\create_and_sync_term( 'tag-name-1', 'tag name 2', '', 'post_tag' ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $term_query = new \WP_Term_Query( + [ + 'taxonomy' => array( 'wptests_tax', 'post_tag' ), + 'ep_integrate' => true, + 'hide_empty' => false, + ] + ); + $this->assertObjectNotHasAttribute( 'elasticsearch_success', $term_query ); + $this->assertEquals( 4, count( $term_query->terms ) ); + } }