I have categories the names of which are 1 to 25. But the WordPress ordering system doesn’t work correctly. It orders them as 1,10,11,12,13…2,21,22,23,24,25. I don’t want to add 0 to 1-9 numbers. How can I fix this issue?

This is my code:

<li class="categoriesx <?php echo print_category_slug( get_the_category( $post->ID) ); ?>" 
    data-category="<?php echo print_category_slug( get_the_category( $post->ID) ); ?>">

2 Answers
2

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 Reply

Your email address will not be published. Required fields are marked *