Preserve order from the Intuitive Custom Post Order plugin

If you’re using the Intuitive Custom Post Order plugin to organize your taxonomy terms, here’s how to preserve that order for your term facets.

Setup

First, edit your facet (Settings > FacetWP) and set the Sort by option to “Term Order”.

Next, add the following code into your (child) theme’s functions.php:

add_filter( 'get_terms_args', function( $args, $taxonomies ) {
    if ( isset( $args['term_order'] ) ) {
        $args['orderby'] = 'term_order';
    }
    return $args;
}, 10, 2 );

add_filter( 'get_terms_orderby', function( $orderby, $query_vars ) {
    return 'term_order' === $query_vars['orderby'] ? 'term_order' : $orderby;
}, 10, 2 );

Why is the above code needed?

Long story short, the above code helps prevent issues with WooCommerce sites. If the orderby is directly set to “term_order”, then WooCommerce (surprisingly) defaults to sorting by date.