Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add new orderby parameter to get_terms in WordPress to allow for natural or alphanumeric sort
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