1

Below is the code I am using to create and make a data sortable column. These data fields are created by Advanced Custom Fields. The dates are not sorting correcting even though they are displayed in a "Ymd" format. Can anyone help me get the dates sorting correctly?

//Add custom column to user list
function new_modify_user_table( $column ) {
    $column['school_name'] = 'School Name';
    $column['shopping_start_date'] = 'Start Date';
    $column['shopping_end_date'] = 'End Date';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'school_name' :
            return get_the_author_meta( 'school_name', $user_id );
        case 'shopping_start_date' :
            return get_the_author_meta( 'shopping_start_date', $user_id );
        case 'shopping_end_date' :
            return get_the_author_meta( 'shopping_end_date', $user_id );
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );

function wpse_user_table_list_sortable_columns($columns) {
    $columns['school_name'] = 'school_name';
    $columns['shopping_start_date'] = 'shopping_start_date';
    $columns['shopping_end_date'] = 'shopping_end_date';
    return $columns;
}
add_filter('manage_users_sortable_columns', 'wpse_user_table_list_sortable_columns');

2 Answers 2

0

You have to add following code after your code to get the dates sorting correctly. It's working correctly for me. Hope it will help.

function change_user_order( $args ){

    if( isset( $args['orderby'] ) ) {

        $args['meta_key'] = $args['orderby'];

    }
  
    return $args;
  
}

add_filter( 'users_list_table_query_args', 'change_user_order' );
0

Solution I used, based on previous answer by Veerji but making sure only the custom columns are affected. Otherwise default columns’ sorting would stop working.

function form_change_user_order($args) {
    if (isset($args['orderby'])) {
        switch ($args['orderby']) {
            case 'school_name':
            case 'shopping_start_date':
            case 'shopping_end_date':
                $args['meta_key'] = $args['orderby'];
            default:
        }
    }
  
    return $args;
}
add_filter('users_list_table_query_args', 'form_change_user_order');

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.