Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using get_terms to output a taxonomy's terms, which are all numbers:

For example:

4, 6, 8, 10, 12

How can I order these numerically, so they appear exactly as above?

What I'm getting is actually:

10, 12, 4, 6, 8

I can see why it's done this (thinks 10 is first because it starts with a 1) but how can I fix it?

I've tried all the ordering options on the codex for get_terms but can't seem to get them in order. Here's an example of what I have so far. I've even tried ordering by id and entering them in order but it still muddled it up.

$taxonomy_array = get_terms('taxonomy-name', 'hide_empty=0');

share|improve this question
    
Why are you using numeric terms? Wouldn't numeric values be better utilised as meta data, ie. post meta.. ? –  t31os Jul 28 '11 at 9:16
    
@t31os to be honest, I have no idea. I just thought this would be the best way to do it. The taxonomy terms above refer to sizing. By having them as taxonomy terms I can now search by them and also maintain the one list. If I need to change the term, I can change it and it will update all the posts linked to it. Is post meta still the way to go in this instance? –  Andrew Jul 28 '11 at 9:31
    
In your case terms may be the better solution because you need a means to group posts, and select those groups of posts, so taxonomies are well suited for that requirement. So in your case, i'd say you made the right decision... :) –  t31os Jul 28 '11 at 10:19
    
Phew :) Thanks for your comments though. Is post meta essentially how custom fields store their info? Ie the info is stored just against that post, because it should only be related to that post. –  Andrew Jul 28 '11 at 11:29
    
Yes that's exactly what custom fields are... post meta. :) –  t31os Jul 28 '11 at 11:48

1 Answer 1

up vote 1 down vote accepted

I have a plugin, Custom Taxonomy Sort, that allows you to sort taxonomy terms in any order that you might want. After installing the plugin, there will be an order field for each term. It will save these values as integers and will properly sort your taxonomy terms. By default, it will use the order you specify to sort the terms. It also enables a new value for sort the orderby parameter, "custom_sort". You would be able to do something like:

<?php $terms = get_terms('taxonomy', 'orderby=custom_sort'); ?>
share|improve this answer
    
Thanks @tollmanz, works perfectly now. Wish I had found this earlier. No doubt I'll need it again very soon so will be keeping your plugin close by. Weird how WP sorts the numbers though.. still can't get my head around that. I have a similar case where I was sorting by custom fields (custom fields were numbers) and had the same problem as this question. Might take a look and see if there is a similar plugin to yours that works with ordering custom fields. –  Andrew Jul 27 '11 at 5:13
1  
This isn't a WP issue. As a string, "10" comes before "4" because it will be evaluated by the first character. With your custom field problem, there are a few ways to handle it. Take a look at the meta_query parameter of the WP_Query class and pay special attention to the type subparameter. You want to compare your values as type "NUMERIC". If you have trouble, post back here. Just realize that this is a general data type issue, not a WP problem. –  tollmanz Jul 27 '11 at 6:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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