How to update WordPress wp_usermeta?

2019-09-19 15:35发布

问题:

I need to update a single column in wordpress wp_usermeta.I have gone through the web and I found these codes

<?php update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ); ?>

Here user_id=1, meta_key=first_name, meta_value="Smith", $prev_value="benny"

How do i write SQL to update the specific column.

回答1:

You don't need to write the SQL, wp gives you that function.

If you are doing a one-off to update the DB, then in your phpmyadmin, you just find the record, and edit it manually;

if writing PHP, then you use

update_user_meta( $user_id, $meta_key, $meta_value, $prev_value )

ie update_user_meta( 1, 'first_name', 'Smith', 'benny' )



回答2:

Use below code to update user meta value if it match old meta value

$user_id = 1;
$meta_key = 'first_name';
$new_value = 'Smith';
$prev_value = 'benny';


update_user_meta( $user_id, $meta_key, $new_value, $prev_value );