Skip to main content
Table name property fix: The Table is $wpdb->users, not $wpdb->wp_users
Source Link
Fayaz
  • 9k
  • 2
  • 32
  • 51

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>users 
            SET creditBalance = %d 
            WHERE ID = %d",
            $creditBalance, 
            $id
        ) 
    );
}

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

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.

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->users 
            SET creditBalance = %d 
            WHERE ID = %d",
            $creditBalance, 
            $id
        ) 
    );
}

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

Source Link
Johansson
  • 15.4k
  • 11
  • 43
  • 79

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.