I want to have the following database entry for a User Meta Field:
meta_key => array ('value 1', 'value 2', 'value 3')
I tried to create the User Meta Field by the first push:
update_user_meta(
$user->id,
meta_key,
array ($value1)
);
Now I want to add new values to the array. But i don’t want to lose the first. How is that possible?
add_user_meta is not working because it adds new database-entries all the time.
The code you’ve shared is a bit mysterious, but I will do my best to give you an answer.
Conceptually, you’ll just want to get the meta first, update it, then rewrite it.
So, once your meta values are written, when you want to update, you would do something like so:
Then you could updated user meta using that function like so:
bonus
At the request of OP, here’s a method to remove a value:
Usage:
Regarding the answer of @cale_b, I had to modify the code a little bit to make it work. Maybe an update since the answer is from 2015:
I found with these functions that they repeatedly nested arrays rather than just having one main array with your values.
This is fixed with the following:
With the main difference being
get_user_meta
set to true: