Support » Developing with WordPress » Updating user meta

  • Hi,

    I’m looking for support with updating all users’ meta with a script once manually. Thanks very much in advance.

    There is user meta: “restrict level”. I want to update all users so that their restrict level is the same as their user group value. They may change later but this would be a great starting point for the new update.

    I think I need to do something like:

    for USER_ID in $(wp user list --role=subscriber --field=ID); do wp user update $USER_ID --restrict level=*; done

    where * means “same as the user group value” for given user:
    eg: group A, group B, group C…

    Can anyone advise me what this line of code should be?

    Bw,
    Ben

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    How many users are involved? Is there anything in the DB that can define who belongs to what group? Most efficient would be to do a SQL INSERT query to add a record for every qualifying user.

    It can be done in a PHP loop, one user at a time. But if a huge number of users are involved, the code could time out before it completes. But if the number is small enough that you could manually compose a user list, you could do something like:

    $users = array( 24, 37, 43, 53, 78,); // user IDs to add meta data to
    $value = 'whatever value to insert';
    foreach ( $users as $user ) {
       $success = update_user_meta( $user, 'restrict_level', $value );
       if ( ! $success ) echo 'Adding meta failed for user ID ', $user, ".<br>\n";
    }

    There is no validation of the user IDs, you could add meta data for users that do not exist if the $users array is not composed carefully. You can put this code on a page template. Every time the page is requested, the code will run. If the data already exists, you will get a list of error messages.

    Thread Starter benorange

    (@benorange)

    Hi @bcworkz,

    Thanks for the fast reply. I appreciate you taking the time.

    How many users are involved?
    There are about 2000 users

    Is there anything in the DB that can define who belongs to what group?
    Yes, user group is a custom field for user added at registration. Users who only book an exam without a course have no assigned value for user group but updating null group to null access level would be ok.

    I’m guessing the process would timeout with the number of users but for my understanding, would the following work if there were only a few users?

    SELECT * FROM users;
    foreach ( users as $user ) {
    	$value = get_user_meta( $user, 'group_id', true );
       	$success = update_user_meta( $user, 'restrict_level', $value );
       	if ( ! $success ) echo 'Adding meta failed for user ID ', $user, ".<br>\n";
    }

    The site has lots of resources but from what you said I’m guessing I would have to break up the list and do them in batches..

    I am clear on the page template -> execute from browser process. I like that plan.

    Bw,
    Ben

    Thread Starter benorange

    (@benorange)

    Note, the access to links granted by access level will be set to expire after 12 months so the users in this list could be filtered by ‘created in in last 12 months’. Then it would ony be a few hundred users..

    Bw,
    Ben

    You will not have a timeout when you call the PHP script via console/bash/shell. There it will run as long as it needs to unless the server shuts down long running processes (depends on hosting).

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.