2

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 1

2

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');
3
  • Thx Samuel. I wasn't expecting an answer after so long! :) Your answer works, but the issue wasn't with the wp functions. It was a plugin called W3 Total Cache which some idiot (me) switched on in our dev instance a few months ago. It was caching db queries!
    – dewd
    Jul 29, 2015 at 8:16
  • You're welcome @dewd . Does this mean you your issue is resolved? And yes, I can imagine the pain caused by caching tools, especially when you don't recognize that they are the issue..
    – Ismail
    Jul 29, 2015 at 13:53
  • Samuel. Yes, it is, but as I suppose there's no correct answer to resolve the issue, I won't mark any as accepted.
    – dewd
    Jul 29, 2015 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.