diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index f00213ef5..d1463c81a 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1000,7 +1000,8 @@ function( $tax_query ) use ( $args ) { $taxonomies = get_taxonomies( array(), 'objects' ); foreach ( $taxonomies as $tax_slug => $tax ) { - if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) ) { + // Exclude the category taxonomy from this check if we are performing a Tax Query as category_name will be set by core + if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && 'category' !== $tax->name ) { $args['tax_query'][] = array( 'taxonomy' => $tax_slug, 'terms' => (array) $args[ $tax->query_var ], diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 190df3c95..7379ee703 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -6196,4 +6196,39 @@ public function testPostPrepareDateTerms() { $this->assertArrayHasKey( 'second', $return_prepare_date_terms ); $this->assertArrayHasKey( 'm', $return_prepare_date_terms ); } + + /** + * Test when we perform a Tax Query with Id's for the category taxonomy cat id is used and cat slug is not. + * + * @return void + * @group post + */ + public function testTaxQueryWithCategoryId() { + $cat = wp_create_category( 'test category' ); + + $query = new \WP_Query(); + + $post = new \ElasticPress\Indexable\Post\Post(); + + $args = $post->format_args( + [ + 'post_type' => 'post', + 'post_status' => 'public', + 'ep_integrate' => true, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( $cat ), + 'field' => 'term_id', + 'operator' => 'in', + ) + ) + ], + $query + ); + + $this->assertCount( 1, $args['post_filter']['bool']['must'][0]['bool']['must'] ); + $this->assertArrayHasKey( 'terms.category.term_id', $args['post_filter']['bool']['must'][0]['bool']['must'][0]['terms'] ); + $this->assertContains( $cat, $args['post_filter']['bool']['must'][0]['bool']['must'][0]['terms']['terms.category.term_id'] ); + } }