#49809 closed defect (bug) (invalid)
Add user meta is saving more values
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Options, Meta APIs | Keywords: | |
Focuses: | Cc: |
Description
On single.php place this function and open single post, meta is working fine, "fero" is saved one time. Then open another blog post, you will see that now it save two times same value "fero".
I believe it should save only one value to array.
<?php add_user_meta(get_current_user_id(),'video_history',"fero");
Change History (3)
#1
@
3 years ago
- Component changed from General to Options, Meta APIs
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
#2
@
3 years ago
Hello @egyptimhotep
This is the expected behaviour. You may either supply en extra parameter $unique and set it to true, or use ´update_user_meta()´. Documented here https://developer.wordpress.org/reference/functions/add_user_meta/ and https://developer.wordpress.org/reference/functions/update_user_meta/
#3
@
3 years ago
Can someone give exaplanation? I need to add post_id value to array, to user meta, but sometimes I get to values into array, and the page was reloaded only one time. It added on every refresh.
<?php // adds the watched ID to the users meta so they can't watch it again function video_history_of_user($user_id, $post_id) { // get options as array, update if is array and save it as option again $watched = get_user_meta('video_history', $user_id, true); if (is_array($watched)) { // if history array contain post id, remove it from history if (($key = array_search($post_id, $watched)) !== false) { unset($watched[$key]); } $watched[] = $post_id; }else{ $watched = array($post_id); } update_user_meta($user_id, 'video_history', $watched); }
Is it bugg or it is my bad.
Thank you.
Hello, @egyptimhotep! Trac is for reporting bugs in WordPress Core, and this is actually a support issue. That being said, try using https://developer.wordpress.org/reference/functions/update_user_meta/ instead.
If you have any further issues, try asking over at the support forums https://wordpress.org/support/forums/.