10

I'm looking at the docs for add_user_meta() vs update_user_meta().

If the current meta_key does not exist for a user, will update_user_meta() automatically add the meta_key for that user or do you have to define the meta_key with add_user_meta() first?

15

You have already found out that using update_user_meta() if the meta field for the user does not exist, it will be added. ie update_user_meta() can do the task of add_user_meta()

However, the difference between them is the return values

update_user_meta()

returns False if no change was made (if the new value was the same as previous value) or if the update failed, umeta_id if the value was different and the update a success.

NOTE: as of v3.4.2 it returns the umeta_id on success (instead of true) and false on failure


add_user_meta()

return Primary key id for success. No value (blank) for failure. Primary key id for success.

  • I wanna add that the add_user_meta cannot update existing user_meta if the unique flag passed in is set to true. – TurtleTread Mar 14 '18 at 15:14
1

Thanks for your explanation, i will just add to yours the update_user_meta() also return true if successful update happened.

In short - It returns Meta ID if the key didn't exist, true on successful update, false on failure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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