$order, 'ID' => 'ASC', ); return $args; } /** * Append the term_id tiebreak to a tie-prone term-query ORDER BY clause. * * The get_terms API has no array-orderby form, so the tiebreak lands at the clause * level. Only `name` can tie (same-named children under different parents); * the direction moves INTO the orderby clause because WP appends the free * `order` to the LAST expression only. * * @param array $clauses WP_Term_Query SQL clauses. * * @return array */ public static function with_term_id_tiebreak( array $clauses ): array { $orderby = (string) ( $clauses['orderby'] ?? '' ); if ( 'ORDER BY t.name' !== $orderby ) { return $clauses; } $order = 'DESC' === strtoupper( (string) ( $clauses['order'] ?? 'ASC' ) ) ? 'DESC' : 'ASC'; $clauses['orderby'] = $orderby . ' ' . $order . ', t.term_id'; $clauses['order'] = 'ASC'; return $clauses; } }