2

I wanted to change the default taxonomy terms order by its 'term_order' value instead of 'name' in admin side. So I tried something like below. But it doesn't work and php memory exhaust.

function uc_order_term( $wp_query ) {
    $wp_query->query(
        array(
            'taxonomy' => 'category',
            'orderby' => 'term_order',
            'order' => 'ASC'
        )
    );
}
add_action( 'pre_get_terms', 'uc_order_term');

However in similar way I tried to sort posts by menu_order and it works.

function uc_order_post( $wp_query ) {

            $wp_query->set('orderby', 'menu_order');
            $wp_query->set('order', 'ASC');

}
add_action( 'pre_get_posts', 'uc_order_post', 1 );

enter image description here

2 Answers 2

1

This should be possible with get_terms_args. The code only changes the order for categories.

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

https://developer.wordpress.org/reference/hooks/get_terms_args/

0

Now I am able to sort taxonomy terms by term_order using below snippets. But I am still seeking answer for the original question "How to do it using pre_get_terms".

function foo_tax_order($orderby, $args){

        return 't.term_order';
}
add_filter('get_terms_orderby', 'foo_tax_order', 10, 2);
3
  • Hey, have you tried something like this: stackoverflow.com/questions/41269101/… ?
    – Pierre
    Commented May 27, 2019 at 17:00
  • @Pierre That link is for posts. I am asking about terms query.
    – SkyRar
    Commented May 27, 2019 at 19:20
  • I know, but I think it will work if you replace pre_get_posts by pre_get_terms and then use $query->set( 'orderby', 'term_order' ); within the function
    – Pierre
    Commented May 29, 2019 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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