I have added new user meta for a given user. Let’s call it title
. Every time I run
$user = get_user_by('login',$username);
update_user_meta($user->ID, 'title','Manager');
The meta is not updated. Instead, a new row gets added to the wp_usermeta
table for this user
-> meta_key
-> meta_value
with a new umeta_id
.
Also, the meta is not available via get_user_meta($user->ID);
I feel I’m missing something in order to get this working correctly. Any ideas?
1 Answer
Try this:
global $current_user;
$user_id = $current_user->ID; // current user ID
$meta = get_user_meta( $user_id, 'title', 'Manager' ); // subject meta
if($meta != '') // updates meta if exists
update_user_meta($user_id, 'title', 'Manager');
else // creates new meta if not exists
add_user_meta($user_id, 'title', 'Manager');