I have data in the metauser table as shown below. As you can see, the data is in the form of a serialized array. How do I push array data? I want to save the post id in the meta bookmark. But if I use add_user_meta()
instead it creates data in a new row instead of adding data in value (so there will be 2 meta bookmarks in 1 user with different values). If I use update_user_meta()
it just replaces the old new value, not adding (push) the array data. How do I add new data (array) to the meta value?
Like the example image below, this is the meta of post_status
and comment_status
from plugin ulike WordPress. I think when there are new posts or new comments, the value will also increase according to the associated id.
please, free to edit this question if there is an incorrect or unsuitable word.
Thank You!
2
Answers
You can use
get_user_meta
function to get value.Remember WordPress uses
maybe_serialize
andmaybe_unserialize
to save and get value from meta tables.To store an array as a user meta in the WordPress database you can use the PHP
serialize()
function. In this example an array will be stored for the current user (as in your screenshot).To modify a serialized array you can use the PHP
unserialize()
function. In this example, the user meta_bookmark_article
will be retrieved, modified and updated: