diff --git a/includes/wc-term-functions.php b/includes/wc-term-functions.php index bc1913513fd..b250de932cb 100644 --- a/includes/wc-term-functions.php +++ b/includes/wc-term-functions.php @@ -34,8 +34,6 @@ function wc_change_get_terms_defaults( $defaults, $taxonomies ) { switch ( $orderby ) { case 'menu_order': - $defaults['orderby'] = 'meta_value_num'; - $defaults['meta_key'] = 'order'; // phpcs:ignore $defaults['force_menu_order_sort'] = true; break; case 'name_num': @@ -60,14 +58,21 @@ add_filter( 'get_terms_defaults', 'wc_change_get_terms_defaults', 10, 2 ); function wc_change_pre_get_terms( $terms_query ) { $args = &$terms_query->query_vars; - if ( ! empty( $args['menu_order'] ) ) { - $args['order'] = 'DESC' === strtoupper( $args['menu_order'] ) ? 'DESC' : 'ASC'; - $args['orderby'] = 'meta_value_num'; - $args['meta_key'] = 'order'; // phpcs:ignore - $args['force_menu_order_sort'] = true; - $terms_query->meta_query->parse_query_vars( $args ); + // When COUNTING, disable custom sorting. + if ( 'count' === $args['fields'] ) { + return; } + if ( ! empty( $args['menu_order'] ) ) { + $args['order'] = 'DESC' === strtoupper( $args['menu_order'] ) ? 'DESC' : 'ASC'; + $args['force_menu_order_sort'] = true; + } + + if ( ! empty( $args['force_menu_order_sort'] ) ) { + $args['orderby'] = 'meta_value_num'; + $args['meta_key'] = 'order'; // phpcs:ignore + $terms_query->meta_query->parse_query_vars( $args ); + } } add_action( 'pre_get_terms', 'wc_change_pre_get_terms', 10, 1 ); @@ -83,7 +88,7 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) { global $wpdb; // No need to filter when counting. - if ( strpos( 'COUNT(*)', $clauses['fields'] ) !== false ) { + if ( strpos( $clauses['fields'], 'COUNT(*)' ) !== false ) { return $clauses; } diff --git a/tests/unit-tests/attributes/functions.php b/tests/unit-tests/attributes/functions.php index 577ece7229a..86392e410ad 100644 --- a/tests/unit-tests/attributes/functions.php +++ b/tests/unit-tests/attributes/functions.php @@ -1,10 +1,14 @@ assertFalse( $result ); } + + /** + * Test counts of attributes. + */ + public function test_count_attribute_terms() { + $global_attribute_data = WC_Helper_Product::create_attribute( 'test', array( 'Chicken', 'Nuggets' ) ); + $count = wp_count_terms( + $global_attribute_data['attribute_taxonomy'], + array( + 'hide_empty' => false, + ) + ); + + $this->assertEquals( 2, $count ); + } }