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.
-
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?– fdsaCommented 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.– RyanCommented Jul 10, 2012 at 4:03
Add a comment
|
1 Answer
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.
-
Thanks! I didn't know about the term_order column. I will see if I can get that to work and report back.– RyanCommented Jul 10, 2012 at 4:04
-
2+1 but this also means, that you are hoping, that core won't use it ever ;)– kaiserCommented Jul 10, 2012 at 5:19
-
1It 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_OCommented Jul 10, 2012 at 6:18