I have made a custom column in user administration panel showing user meta data and registered it as sortable. Made a query action to sort it by the meta numerical value (the stored user meta data is numbers) but the sorting is still wrong (does not sort by the meta value at all - I suspect it sorts by user name or date instead).

Can anyone point me in the correct direction please?

My code:

// Add new column to users list
add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
    $columns['billing_vat'] = 'IČO';
    return $columns;

add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user1 = get_user_meta( $user_id, 'billing_vat', true );
    if ( 'billing_vat' == $column_name )
        return $user1;
    return $value;
// Add sortability to the new column in users list   
 function billing_vat_column_register_sortable( $columns ) {
   $columns['billing_vat'] = 'IČO';
   return $columns;

add_filter( 'manage_users_sortable_columns', 'billing_vat_column_register_sortable' );

function wpse_208315_sort_by__billing_vat( $query ) {
   if ( $query->is_main_query() && $query->get( 'orderby' ) === 'IČO' ) {
        $query->set( 'meta_key', 'billing_vat' /* Post meta field name of status */ );
        $query->set( 'orderby', 'meta_value_num' );

add_action( 'pre_get_posts', 'wpse_208315_sort_by_billing_vat' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.