Skip to content

Instantly share code, notes, and snippets.

@clifgriffin
Created August 30, 2013 13:23
Show Gist options
  • Save clifgriffin/6389814 to your computer and use it in GitHub Desktop.
Save clifgriffin/6389814 to your computer and use it in GitHub Desktop.
Add new orderby parameter to get_terms in WordPress to allow for natural or alphanumeric sort
<?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