0

Hey guys, I have this points system on a project, everything works fine except that I can't add meta_values to multiple users.

I'm using <?php update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ) ?> but this seems to not work with multiple users.

Any idea?

0

3 Answers 3

5

Do you simply pass an array with user_ids to update_user_meta? Then it won't work indeed.

Using a loop over all user_ids it should work:

foreach($user_ids as $user_id){
    update_user_meta($user_id, $key, $value);
}

Think about the SQL being generated, while keeping in mind the database design of WordPress. It has the table wp_usermeta, with columns user_id, meta_key and meta_value. That means that every meta value has a row in this table, for every user. You could use some code as

global $wpdb;

$ids = implode("', '", $user_ids);
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->usermeta} SET meta_value = %s WHERE user_id IN ('{$ids}') AND meta_key = %s"), $value, $key);

However this would not work if the meta_key for a certain user does not exist yet. You would then need an INSERT statement, which you can't do in a batch.

1
  • Please post some more code then, or try to debug it yourself.
    – Joost
    Dec 31, 2010 at 15:18
2

You can try something like;

$user_meta_fields = array ( 'first_name', 'last_name', 'account_type', 'vd_sub_name', 'description', 'vd_adress', 'vd_cp', 'vd_ville', 'vd_department', 'vd_region', 'vd_country', 'vd_tel', 'vd_gsm', 'vd_fax', 'url' );
        foreach($user_meta_fields as $user_meta_field){
            //echo "update_user_meta(".$user_id.", ".$user_meta_field.", ".$posted[$user_meta_field].");";
            update_user_meta($user_id, $user_meta_field, $posted[$user_meta_field]);
        }
0

Here is something that I made:

/*
 * Optimized way to add multiple usermeta keys and values
 * @author Ivijan-Stefan Stipic
 */
if( !function_exists('bulk_update_user_meta') ) : function bulk_update_user_meta(int $user_id, array $meta_data) {
    global $wpdb;

    $transactions = 0;

    foreach ($meta_data as $meta_key => $meta_value) {
        $meta_data[$meta_key] = [
            'exists' => $wpdb->get_var($wpdb->prepare(
                "SELECT COUNT(*) FROM `{$wpdb->usermeta}` WHERE `user_id` = %d AND `meta_key` = %s",
                $user_id,
                $meta_key
            )) > 0,
            'value' => maybe_serialize($meta_value)
        ];
    }

    $wpdb->query('START TRANSACTION');

    foreach ($meta_data as $meta_key => $meta_object) {

        $meta_value = $meta_object['value'];

        if ( $meta_object['exists'] ) {
            if( $wpdb->query($wpdb->prepare(
                "UPDATE `{$wpdb->usermeta}` SET `meta_value` = %s WHERE `user_id` = %d AND `meta_key` = %s",
                $meta_value,
                $user_id,
                $meta_key
            )) !== false ) $transactions++;
        } else {
            if( $wpdb->query($wpdb->prepare(
                "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES (%d, %s, %s)",
                $user_id,
                $meta_key,
                $meta_value
            )) !== false ) $transactions++;
        }
    }

    $wpdb->query('COMMIT');

    clean_user_cache($user_id);

    return $transactions === count($meta_data);
} endif;

It is simple to use:

bulk_update_user_meta( $user_id, [
    'ivi_test__1' => mt_rand( 1, 100 ),
    'ivi_test__2' => mt_rand( 1, 100 ),
    'ivi_test__3' => mt_rand( 1, 100 ),
    'ivi_test__4' => mt_rand( 1, 100 ),
    'ivi_test__5' => mt_rand( 1, 100 ),
    'ivi_test__6' => mt_rand( 1, 100 ),
    'ivi_test__7' => mt_rand( 1, 100 ),
    'ivi_test__8' => mt_rand( 1, 100 ),
    'ivi_test__9' => mt_rand( 1, 100 ),
    'ivi_test__10' => mt_rand( 1, 100 ),
    'ivi_test__11' => mt_rand( 1, 100 ),
    'ivi_test__12' => mt_rand( 1, 100 ),
] );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.