Advanced Taxonomy Term Ordering - YIKES WordPress Plugins

Knowledge Base

Advanced Taxonomy Term Ordering

Article Last Updated: July 22, 2020

Do you want to order your taxonomy terms differently in multiple locations?

For example, let’s say you have a list of post categories ordered like so with Simple Taxonomy Ordering:

  • Fiction
  • Non-Fiction
  • Cook Books

This might be the order you want those terms to display, except for in one location. Let’s say you want them to be ordered by name in one location.

Here’s how you would reorder your taxonomy terms by name for one location in your theme, and then restore the custom order once you’re done.

remove_filter( 'terms_clauses', [ Yikes_Custom_Taxonomy_Order::get_instance(), 'set_tax_order' ], 10, 3 );

wp_list_categories( [
   'orderby' => 'name',
   'order'   => 'ASC',
] );

add_filter( 'terms_clauses', [ Yikes_Custom_Taxonomy_Order::get_instance(), 'set_tax_order' ], 10, 3 );

The code above would be placed in your theme where you want the alternately ordered taxonomy to display.

  1. Remove the filter which controls the custom taxonomy ordering.
  2. Next we’re using wp_list_categories to order our categories by name.
  3. Finally, we’re restoring our filter and any of our calls to wp_list_categories will be our custom order

Stay updated on YIKES Plugins