admin管理员组

文章数量:1134246

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');

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');
Share Improve this question edited Sep 8, 2023 at 11:53 Cazuma Nii Cavalcanti 1074 bronze badges asked Nov 20, 2020 at 18:20 SSSlippySSSlippy 132 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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' );

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');

本文标签: Custom date column in user table not sorting correctly