Add values to User Meta Field within an array

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:

Read More
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.

Related posts

3 comments

  1. 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:

    // Lets create a reusable function for simplicity
    /*
     * @param int $user id
     * @param string $meta_key
     * @param string $new_value - the new value to be added to the array
     */
    function my_meta_update($user_id, $meta_key, $new_value) {
        // Get the existing meta for 'meta_key'
        $meta = get_user_meta($user_id, $meta_key, false);
        // Do some defensive coding - if it's not an array, set it up
        if ( ! array($meta) ) {
            $meta = array();
        }
        // Push a new value onto the array
        $meta[] = $new_value;
        // Write the user meta record with the new value in it
        update_user_meta($user_id, $meta_key, $meta);
    }
    

    Then you could updated user meta using that function like so:

    // Add the "Value 2" to the array of meta values for user 1
    my_meta_update(1, 'my_meta_key', 'Value 2');
    

    bonus
    At the request of OP, here’s a method to remove a value:

    /**
     * @param int $user_id
     * @param string $meta_key
     * @param string $remove_value - the value to remove from the array
     */
    function my_meta_remove($user_id, $meta_key, $remove_value) {
        $meta = get_user_meta($user_id, $meta_key, false);
        // Find the index of the value to remove, if it exists
        $index = array_search($remove_value, $meta);
        // If an index was found, then remove the value
        if ($index !== FALSE) {
            unset($meta[$index]);
        }
        // Write the user meta record with the removed value
        update_user_meta($user_id, $meta_key, $meta);
    }
    

    Usage:

    // Remove "Value 2" from the array of meta values for user 1
    my_meta_remove(1, 'my_meta_key', 'Value 2');
    
  2. 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:

    // Do some defensive coding - if it's not an array, set it up
    if ( ! is_array($meta) ) {
        $meta = array();
    }
    
  3. 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:

    function my_meta_update($user_id, $meta_key, $new_value) {
        // Get the existing meta for 'meta_key'
        $meta = get_user_meta($user_id, $meta_key, true);
        // Do some defensive coding - if it's not an array, set it up
        if($meta == '') {
            $meta = array();
        }
        // Push a new value onto the array
        array_push($meta, $new_value);
        // Write the user meta record with the new value in it
        update_user_meta($user_id, $meta_key, $meta);
    }
    
    function my_meta_remove($user_id, $meta_key, $remove_value) {
        $meta = get_user_meta($user_id, $meta_key, true);
        // Find the index of the value to remove, if it exists
        $index = array_search($remove_value, $meta);
        // If an index was found, then remove the value
        if ($index !== FALSE) {
            unset($meta[$index]);
        }
        // Write the user meta record with the removed value
        update_user_meta($user_id, $meta_key, $meta);
    }
    

    With the main difference being get_user_meta set to true:

    $meta = get_user_meta($user_id, $meta_key, true);
    

Comments are closed.