WordPress:按数字排序管理员用户列

WordPress: Order admin user column by number

我想按自定义 ID 对用户概览列表中的管理栏进行排序。 为此,我添加了一个函数来对列进行排序,但它没有按预期工作。

它以一种奇怪的方式对字段进行排序。我想还有其他排序或函数有错误?!

这是给用户添加ID的代码:

add_action( 'edit_user_profile', 'wcv_store_custom_vendor_id', 0 );
//add_action( 'wcv_admin_after_store_address', 'wcv_store_custom_vendor_id' );
function wcv_store_custom_vendor_id( $user ) {
?>
    <table class="form-table">
        <tbody>
            <tr>
                <th><label for="_wcv_custom_vendor_id"><?php _e( 'Vendor ID', 'wcvendors-pro' ); ?></label></th>
                <td><input type="text" name="_wcv_custom_vendor_id" id="_wcv_custom_vendor_id" value="<?php echo get_user_meta( $user->ID, '_wcv_custom_vendor_id', true ); ?>" class="regular-text"></td>
            </tr>
        </tbody>
    </table>
<?php
}



// Save the details on the back end when updating the user
add_action( 'wcvendors_update_admin_user', 'save_wcv_custom_vendor_id' );
function save_wcv_custom_vendor_id( $user_id ){
    if ( isset( $_POST['_wcv_custom_vendor_id'] ) ) {
        update_user_meta( $user_id, '_wcv_custom_vendor_id', $_POST['_wcv_custom_vendor_id'] );
    }

这是我目前的情况:

// Add column to admin
add_action('manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
function add_custom_vendor_id_column( $columns ) {
    $columns['wcv_custom_vendor_id'] = __('Vendor-ID');
    return $columns;
}


// fetching the status, thanks to LoicTheAztec
add_filter('manage_users_custom_column',  'add_data_to_vendor_id_column', 10, 3);
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
    if ( 'wcv_custom_vendor_id' == $column_name ) {
        if( get_user_meta( $user_id, '_wcv_custom_vendor_id', true ) != '' ) {
            $value = '<span style="color:green;font-weight:bold;">'.get_user_meta( $user_id, '_wcv_custom_vendor_id', true ).'</span>';
        } else {
            $value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
        }
    }
    return $value;
}

// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
    $columns['wcv_custom_vendor_id'] = 'Vendor-ID';
    return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' );

// second try to sort by number, no effect?!
function sort_datanowa_column( $vars ) {
    if ( isset( $vars['orderby'] ) && 'Vendor-ID' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => '_wcv_custom_vendor_id', // Find correct meta field key
            'orderby' => 'meta_value_num'
        ) );
    }
    return $vars;
}
add_filter( 'request', 'sort_datanowa_column' );

我检查了很多片段,最后一个函数应该按数字排序。但是我觉得一点作用都没有。

有什么想念的吗?

  • 使用 pre_get_users 相反 request
  • 对于meta_key的空值我们使用如下:update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );

所以你得到:

// Add column to admin
function add_custom_vendor_id_column( $columns ) {  
    $columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
    
    return $columns;
}
add_action( 'manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );

// fetching the status, thanks to LoicTheAztec
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
    if ( $column_name == 'wcv_custom_vendor_id' ) {
        
        $vendor_id = (int) get_user_meta( $user_id, '_wcv_custom_vendor_id', true );
        
        // If empty
        if ( empty ($vendor_id ) ) {
            update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
        }
        
        if ( $vendor_id && $vendor_id != 0 ) {          
            $value .= '<span style="color:green;font-weight:bold;">' . $vendor_id . '</span>';
        } else {
            $value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
        }
    }
    
    return $value;
}
add_filter( 'manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);

// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {   
    $columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
    
    return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column', 10, 1 );

// orderby
function action_pre_get_users( $query ) {
    if ( !is_admin() )
        return;

    $orderby = $query->get('orderby');

    if ( $orderby == 'Vendor-ID' ) {
        $query->set( 'orderby' , 'meta_value_num' );
        $query->set( 'meta_key', '_wcv_custom_vendor_id' );
    }
}
add_action( 'pre_get_users', 'action_pre_get_users', 10, 1 );