Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#49809 closed defect (bug) (invalid)

Add user meta is saving more values

Reported by: egyptimhotep Owned by:
Priority: normal Milestone:
Component: Options, Meta APIs Version:
Severity: normal Keywords:
Cc: Focuses:

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 @davidbaumwald
7 years ago

  • Component General → Options, Meta APIs
  • Milestone Awaiting Review
  • Resolution → invalid
  • Status new → closed

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/.

#2 @knutsp
7 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 @egyptimhotep
6 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.

Last edited 6 years ago by egyptimhotep (previous) (diff)
Note: See TracTickets for help on using tickets.