1

I've been looking all over for a way to update all of my site's User Accounts meta data with both built in WP functions update_user_meta and add_user_meta. I've tried the suggested solutions listed here and here but neither work. I believe I read that the functions do not allow a way to update mutliple users meta data at once so that could be why the methods I've tried earlier failed. Unfortunately, this is precisely what I need to do. Any fix suggestions or alternative methods? Below are code snippets of my earlier attempts.

Attempt 1:

$args = array (
    'fields'         => 'all_with_meta',
    'role' => 'Participant',
    //'number' => 50

);

// The User Query
$user_query = new WP_User_Query( $args );

$profile_zipcode = $registers[$x]["18"];
    $profile_state = $registers[$x]["23"];
    $users = $user_query->get_results();

    if (!empty($users)) {

        foreach ($users as $user)
        {
            add_user_meta($user->id, 'profile_zipcode', $profile_zipcode);
            add_user_meta($user->id, 'profile_state', $profile_statue);
        }
    }

Attempt 2:

$total_registers = 0;
$registers = GFAPI::get_entries(
        $form_id3,
        $register_search_criteria = array('status' => 'active'),
        $register_sorting = null,
        $register_paging = array( 'offset' => 0, 'page_size' => 100 ),
        $total_registers );

ini_set('max_execution_time', 900);
for ($x = 0; $x <= $total_registers; $x++) {
    $profile_zipcode = $registers[$x]["18"];
    $profile_state = $registers[$x]["23"];


    $metas = array(
            'profile_zipcode' => $profile_zipcode,
            'profile_state' => $profile_state
            );

        foreach($metas as $key => $value) {
            add_user_meta( $main_id, $key, $value);
        }
}

Thanks in advance!

Edit:

I still haven't managed to find a solution. I simply want to run a standard update_user_meta() function for multiple users.

For example

foreach ( $gotten_users as $user ) {   
        $user_id = $user->ID;
        $user_update = update_user_meta($user_id, 'key', $key_value);
    }

I have searched and tried many different things to no avail. Any help would be greatly appreciated.

Community
  • 1
  • 1
gabed123
  • 515
  • 3
  • 8
  • 19

1 Answers1

3

You should be able to use get_users().

Would look something like this:

$users = get_users( ['fields' => ['ID'] ] );
foreach ( $users as $user ) {
    $user_update = update_user_meta($user->ID, 'key', $key_value);
}
kunruh
  • 874
  • 1
  • 8
  • 17