Fix group clause depending on query

Fixes #15394
This commit is contained in:
Mike Jolley 2017-06-01 13:33:30 +01:00
parent d161ba0216
commit e3a4008d9f
1 changed files with 10 additions and 4 deletions

View File

@ -507,12 +507,14 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
// WordPress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes.
$found = false;
foreach ( (array) $taxonomies as $taxonomy ) {
if ( taxonomy_is_product_attribute( $taxonomy ) || in_array( $taxonomy, apply_filters( 'woocommerce_sortable_taxonomies', array( 'product_cat' ) ) ) ) {
$found = true;
break;
}
}
if ( ! $found ) {
return $clauses;
}
@ -534,9 +536,6 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
$clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON (t.term_id = tm.term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') ";
}
// Grouping.
$clauses['where'] .= ' GROUP BY t.term_id ';
// Default to ASC.
if ( ! isset( $args['menu_order'] ) || ! in_array( strtoupper( $args['menu_order'] ), array( 'ASC', 'DESC' ) ) ) {
$args['menu_order'] = 'ASC';
@ -550,10 +549,17 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
$clauses['orderby'] = $order;
}
// Grouping.
if ( strstr( $clauses['fields'], 'tr.object_id' ) ) {
$clauses['orderby'] = ' GROUP BY t.term_id, tr.object_id ' . $clauses['orderby'];
} else {
$clauses['orderby'] = ' GROUP BY t.term_id ' . $clauses['orderby'];
}
return $clauses;
}
add_filter( 'terms_clauses', 'wc_terms_clauses', 10, 3 );
add_filter( 'terms_clauses', 'wc_terms_clauses', 99, 3 );
/**
* Function for recounting product terms, ignoring hidden products.