Sort a custom post type array numerically

We’ve set up drop down lists to allow people to filter posts based on custom post types.

In order to populate the drop down list I am using:

<?php $terms = get_terms("ratios");
$count = count($terms);
if ( $count > 0 ){
 foreach ( $terms as $term ) {
     echo "<option value="" . $term->slug . "">" . $term->name . "</option>";
 }
}?>
</select>

The slug and the name are pretty similar, so I don’t mind which one we are using.

How can I list them as integers?

The site is here if you need more of an idea: http://amigaeng.com.au/gearbox-list/

1 Answer
1

get_terms function will return an array of object. Each of them has term_id field which contains id of a term. Just use it instead of slug and you will have what you need:

<?php $terms = get_terms("ratios");
if ( $count($terms) > 0 ){
 foreach ( $terms as $term ) {
     echo "<option value="" . $term->term_id . "">" . $term->name . "</option>";
 }
}?>
</select>

Leave a Comment