I'm using the following code to display an archive dropdown:

    wp_dropdown_categories( 'taxonomy=week&hierarchical=1&orderby=name' );

However the format of the taxonomy is week-1, week-2 .... week-10, week-11 I need it to be natural orderered as per http://www.php.net/manual/en/function.natsort.php e.g.

Week 1
Week 2
Week ...
Week 10
Week 11

Currently it's being ordered true alpha e.g.

Week 1
Week 10
Week 11
Week 2

Not sure what the best way to do this is, any help or thoughts much appreciated.

1 Answer 1


not the best way (at least if you have a lot of tags)

add_filter('get_terms_orderby', 'get_terms_orderby_natural_slug',10,2);
wp_dropdown_categories( 'taxonomy=week&hierarchical=1&orderby=slug' );
remove_filter('get_terms_orderby', 'get_terms_orderby_natural_slug');

function get_terms_orderby_natural_slug($orderby, $args){
    $orderby = "SUBSTR({$orderby} FROM 1 FOR 5), CAST(SUBSTR({$orderby} FROM 6) AS UNSIGNED), SUBSTR({$orderby} FROM 6)";
    return $orderby;

but still this is a way to do that...

  • This is an interesting solution, however numbers less than 10 are sorted inverse, e.g. week 9, week 8, week ..., week 1, week 10
    – Jona
    Commented Nov 14, 2012 at 15:07
  • should work now. Commented Nov 14, 2012 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.