3

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.

2
  • 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
    Commented Jul 10, 2012 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
    Commented Jul 10, 2012 at 4:03

1 Answer 1

5

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.

3
  • Thanks! I didn't know about the term_order column. I will see if I can get that to work and report back.
    – Ryan
    Commented Jul 10, 2012 at 4:04
  • 2
    +1 but this also means, that you are hoping, that core won't use it ever ;)
    – kaiser
    Commented Jul 10, 2012 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
    Commented Jul 10, 2012 at 6:18

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.