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 am using wp_list_categories to return (you guessed it) a list of categories within a custom taxonomy. The orderby parameter accepts sorting by ID, name, slug, count, and term_group. Is there a way that I can custom sort the results? Ideally I could pass an array of category ID's in the desired order, but obviously I'll have to dig a bit deeper to get this sorted.

share|improve this question
    
when you say post ids do you mean that you want to get the categories of those posts or do you mean that you want to pass category ids? –  fdsa Jul 10 '12 at 3:09
    
My mistake, I meant category ID's, not post ID's. It's been a long day, ha. Let me fix the answer. –  Ryan Jul 10 '12 at 4:03

1 Answer 1

up vote 5 down vote accepted

There is an unused column, term_order, in the wp_term_relationships table that you can use to assign a custom order to the terms within your taxonomy. The order is set at 0 by default and it will take a custom query to get the order back and another solution to set the order.

Example query:

function wpse_order_taxes() {
        global $wpdb;
        $results = $wpdb->get_results ( "SELECT * FROM $wpdb->terms t inner join $wpdb->term_taxonomy tt on t.term_id = tt.term_id WHERE taxonomy = 'category'  ORDER BY term_order ASC LIMIT 0, 10" );
        $categories = array();
        foreach ( $results as $cat ) {
            array_push( $categories, $cat->slug );
        }
        return $categories;
    }

This would give you back an array of terms ordered by the custom order.

share|improve this answer
    
Thanks! I didn't know about the term_order column. I will see if I can get that to work and report back. –  Ryan Jul 10 '12 at 4:04
1  
+1 but this also means, that you are hoping, that core won't use it ever ;) –  kaiser Jul 10 '12 at 5:19
1  
It would be nice if it got used to order categories. It's actually used for galleries. A few trac tickets on it: core.trac.wordpress.org/ticket/9547#comment:7 –  Chris_O Jul 10 '12 at 6:18

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.