Use wp_update_user to update custom column in wp_users table

I’ve added a new column to wp_users table. Now I’d like to update it with wp_update_user(), however though the method returns no errors, no value is being saved. wp_get_current_user is returning the new column, but the value is empty.

Does it mean wp_update_user() can handle only standard columns? Couldn’t find anything about that in docs.

I am aware of the meta key-value table for user defined columns, but it would be more convenient to have new field in user table in my scenario.

The code is simple:

$user_info = wp_get_current_user();
$user_info->creditBalance = 44;
$user_id = wp_update_user( $user_info ); // doesn't seem to update the creditBalance

Solutions Collecting From Web of "Use wp_update_user to update custom column in wp_users table"

It is not a good practice to add/remove columns to/from the core tables in WordPress. There is no guarantee that the very next update doesn’t change this table’s behavior, or even overwrite your column. Your best approach is to use metadata as you mentioned.

But, if you need to do this for some reason, you can do it by writing your own SQL query.

// Get current user's data
$user_info = wp_get_current_user();
// Get its ID
$id = $user_info->ID;
// Set the credit balance
$creditBalance = 44;

// Update the custom column
function update_custom_column( $id, $creditBalance ) {
    global $wpdb;   
    $wpdb->query( 
        $wpdb->prepare( "
            UPDATE $wpdb->wp_users 
            SET creditBalance = %d 
            WHERE ID = %d",
            $creditBalance, 
            $id
        ) 
    );
}

However, working with Database is tricky, and can be dangerous. Be careful.