4

I am currently using WordPress' wp_list_categories function in order to retrieve my terms from a specified taxonomy. I like this function for its ability to generate (hierarchical) HTML lists, that are equipped with proper class names, etc.

I have sorted my terms using the Simple Custom Post Order plugin, which works fine when I'm listing all terms the regular way (i.e. using get_terms).

However, whenever I try to list my terms using wp_list_categories, somehow it does not accept the "orderby" argument when using "term_order" (or "menu_order").

Could somebody point me in the right direction here?

I've provided my code below:

$categories = wp_list_categories([
     'taxonomy' => 'news_category',
     'title_li' => '',
     'orderby' => 'menu_order',
     'order' => 'ASC',
     'echo' => 0,
     'current_category' => $current_term_id,
     'depth' => 0
]);

Edit: I used menu_order, while I should have been using term_order. Still, I hope this serves somebody, some day :)

3

The wp_list_categories() function calls the get_categories() function, that's a wrapper for the get_terms() function, that creates an instance of the WP_Term_Query class. It doesn't look like it supports ordering by term order.

If the plugin uses the term_order column in the wp_terms table, then you can try to add a support for it's ordering, via the get_terms_orderby filter:

add_filter( 'get_terms_orderby', function( $orderby, $qv, $taxonomy )
{
    // Only target the category taxonomy
    if( 'category' !== $taxonomy )
        return $orderby;

    // Support orderby term_order
    if( isset( $qv['orderby'] ) && 'term_order' === $qv['orderby'] )
        $orderby = 't.term_order';

    return $orderby;
}, 10, 3 );

where we only support this for the category taxonomy.

Another approach is to add the filter and remove it right after your wp_list_categories() call.

  • 1
    Thanks for your response @birgire, I will try this out tomorrow and get back to you... Have a nice evening. – digifrog Oct 18 '16 at 15:40
  • Thanks again for your answer. Apparently I was using 'menu_order', rather than 'term_order'. Once I used the latter, it worked. And yes, the answer was obviously on my question. Sorry for wasting your time man! :P – digifrog Oct 19 '16 at 20:36
  • Glad to hear you sorted it out. I understood from your question that you had tested both term_order and menu_order ;-) PS: It might be interesting to see how the plugin is storing the term order and how it sorts the terms. One can also wonder why get_terms()/WP_Term_Query doesn't support term_order ordering by default. @emozio – birgire Oct 19 '16 at 20:40
  • Yes, apparently I was running some inconsistent tests back then, which resulted in false postivies, sort to speak. ;-) – digifrog Oct 20 '16 at 12:37
  • Happens to the best of us @emozio ,-) – birgire Oct 20 '16 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.