Add new orderby parameter to get_terms in WordPress to allow for natural or alphanumeric sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('get_terms', 'sort_terms_naturally', 20, 3); | |
function sort_terms_naturally ( $terms, $taxonomies, $args ) { | |
if ( isset($args['orderby']) && $args['orderby'] == 'natural' ) { | |
$sort_terms = array(); | |
foreach($terms as $term) { | |
$sort_terms[$term->name] = $term; | |
} | |
uksort( $sort_terms, 'strnatcmp'); | |
if ( strtolower($args['order']) == "desc") $sort_terms = array_reverse($sort_terms); | |
return $sort_terms; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment