Skip to content

Commit

Permalink
More work with term ORDER BY when field is not present in SELECT
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickebates committed May 30, 2022
1 parent 505dd6a commit b3feacd
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions wp-includes/class-wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,15 +739,19 @@ public function get_terms() {
$where = "WHERE $where";
}

$this->sql_clauses['select'] = "SELECT $distinct $fields $orderby_fields";
$this->sql_clauses['select'] = "SELECT $fields";
$this->sql_clauses['from'] = "FROM $wpdb->terms AS t $join";
$this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
$this->sql_clauses['limits'] = $limits;
if ( $distinct ) {
$groupby = "group by $fields $orderby_fields";
}

$this->request = "
{$this->sql_clauses['select']}
{$this->sql_clauses['from']}
{$where}
{$groupby}
{$this->sql_clauses['orderby']}
{$this->sql_clauses['limits']}
";
Expand Down Expand Up @@ -914,13 +918,13 @@ protected function parse_orderby( $orderby_raw, &$orderby_fields ) {

if ( in_array( $_orderby, array( 'term_id', 'name', 'slug', 'term_group' ), true ) ) {
$orderby = "t.$_orderby";
//$orderby_fields = ", t.$_orderby";
$orderby_fields = ", t.$_orderby";
} elseif ( in_array( $_orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id', 'description' ), true ) ) {
$orderby = "tt.$_orderby";
//$orderby_fields = ", tt.$_orderby";
$orderby_fields = ", tt.$_orderby";
} elseif ( 'term_order' === $_orderby ) {
$orderby = 'tr.term_order';
//$orderby_fields = ', tr.term_order';
$orderby_fields = ', tr.term_order';
} elseif ( 'include' === $_orderby && ! empty( $this->query_vars['include'] ) ) {
$include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) );
$orderby = "FIELD( t.term_id, $include )";
Expand All @@ -931,10 +935,10 @@ protected function parse_orderby( $orderby_raw, &$orderby_fields ) {
$orderby = '';
} elseif ( empty( $_orderby ) || 'id' === $_orderby || 'term_id' === $_orderby ) {
$orderby = 't.term_id';
//$orderby_fields = ', t.term_id';
$orderby_fields = ', t.term_id';
} else {
//$orderby = 't.name';
//$orderby_fields = ', t.name';
$orderby = 't.name';
$orderby_fields = ', t.name';

// This may be a value of orderby related to meta.
$maybe_orderby_meta = true;
Expand Down

0 comments on commit b3feacd

Please sign in to comment.