How to make custom column Admin>Users sortable?


I have a few hundred users (customers) to delete, specifically those that registered before 2019 and have zero orders. I’ve added the following code to my child theme functions file in order to add a custom column to Admin>Users showing the number of orders. This will enable me to select multiple and bulk delete.

While the custom column is visible and populated, it’s not sortable. Clicking the header ‘Order count’ sorts the table by username alphabetically. I’ve spent hours on this and just can’t crack it. Any help would be much appreciated, how do I make the table sortable by the value in the orders column?
Code taken from Hectors answer to another question found here:
How to delete users with no order history through phpmyadmin sql?

add_filter( 'manage_users_columns', 'prefix5487_modify_user_columns' );

function prefix5487_modify_user_columns( $column ) {
    $column['orders'] = __( 'Order count' );
    return $column;

add_filter( 'manage_users_custom_column', 'prefix5487_user_order_column_value', 10, 3 );

function prefix5487_user_order_column_value( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'orders' :
            return wc_get_customer_order_count( $user_id );
    return $val;

add_filter( 'manage_users_sortable_columns', 'prefix5487_make_registered_column_sortable' );

function prefix5487_make_registered_column_sortable( $columns ) {
    return wp_parse_args( array( 'orders' => 'orders' ), $columns );
Cleopatra 1 month 0 Answers 10 views 0

Leave an answer