diff --git a/includes/classes/Feature/Facets/Widget.php b/includes/classes/Feature/Facets/Widget.php index 45e76fed40..b79c7b5a8a 100644 --- a/includes/classes/Feature/Facets/Widget.php +++ b/includes/classes/Feature/Facets/Widget.php @@ -217,7 +217,7 @@ public function widget( $args, $instance ) { $flat_ordered_terms[] = $top_of_tree; - $to_process = $this->order_by_selected( $top_of_tree->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'] ); + $to_process = $this->order_by_selected( $top_of_tree->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'], $order, $orderby ); while ( ! empty( $to_process ) ) { $term = array_shift( $to_process ); @@ -225,7 +225,7 @@ public function widget( $args, $instance ) { $flat_ordered_terms[] = $term; if ( ! empty( $term->children ) ) { - $to_process = array_merge( $this->order_by_selected( $term->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'] ), $to_process ); + $to_process = array_merge( $this->order_by_selected( $term->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'] ), $to_process, $order, $orderby ); } } @@ -316,12 +316,14 @@ public function widget( $args, $instance ) { /** * Order terms putting selected at the top * - * @param array $terms Array of terms - * @param array $selected_terms Selected terms + * @param array $terms Array of terms + * @param array $selected_terms Selected terms + * @param string $order The order to sort from. Desc or Asc. + * @param string $orderby The orderby to sort items from. * @since 2.5 * @return array */ - private function order_by_selected( $terms, $selected_terms ) { + private function order_by_selected( $terms, $selected_terms, $order = false, $orderby = false ) { $ordered_terms = []; $terms_by_slug = []; @@ -329,10 +331,7 @@ private function order_by_selected( $terms, $selected_terms ) { $terms_by_slug[ $term->slug ] = $term; } - ksort( $selected_terms ); - ksort( $terms_by_slug ); - - foreach ( $selected_terms as $term_slug => $nothing ) { + foreach ( $selected_terms as $term_slug ) { if ( ! empty( $terms_by_slug[ $term_slug ] ) ) { $ordered_terms[ $term_slug ] = $terms_by_slug[ $term_slug ]; } @@ -344,6 +343,30 @@ private function order_by_selected( $terms, $selected_terms ) { } } + if ( 'count' === $orderby ) { + if ( 'asc' === $order ) { + uasort( + $ordered_terms, + function( $a, $b ) { + return $a->count > $b->count; + } + ); + } else { + uasort( + $ordered_terms, + function( $a, $b ) { + return $a->count < $b->count; + } + ); + } + } else { + if ( 'asc' === $order ) { + krsort( $ordered_terms ); + } else { + ksort( $ordered_terms ); + } + } + return array_values( $ordered_terms ); } diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index f00213ef5e..d1463c81a1 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 190df3c958..d136c1563b 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -9,6 +9,7 @@ use ElasticPress; use ElasticPress\Indexables as Indexables; +use function ElasticPressTest\Functions\create_and_sync_term; /** * Test post indexable class @@ -6196,4 +6197,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'] ); + } }