WordPress Ordering Problem. How to fix ordering 1-10-100 issue?

Based on the research I’ve done on this, using term meta data is a better all around approach to ordering terms.

However, I was able come up with this snippet which does sort the terms by their name, numerically:

add_filter( 'terms_clauses', 'wpse247680_terms_clauses', 10, 3 );
function wpse247680_terms_clauses( $pieces, $taxonomies, $args ) {
    // Bail if we are not looking at the right taxonomy, 'category' in this case.
    if ( ! in_array( 'category', $taxonomies ) ) {
        return $pieces;
    }

    // Casts the term name to an integer
    // Idea derrived from similar idea using posts here: https://www.fldtrace.com/custom-post-types-numeric-title-order
    $pieces['orderby'] = 'ORDER BY (t.name+0) ';

    return $pieces;
} 

Here’s a screenshot showing this in action in the admin area. For testing, I created a new taxonomy named ‘numeral’, and created the terms in an arbitrary order. When the code posted above is used, the terms will be ordered numerically.
sort terms numerically by name

Leave a Comment