How can I custom order the results from wp_list_categories?

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.

Related posts

Leave a Reply

1 comment

  1. 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.