add_user_meta() vs update_user_meta()

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x