Updating User Meta with SQL Query

How can I write a bulk MySQL command to add in the value
wp_capabilites=”a:1:{s:10:”subscriber”;b:1;}” into each user_id except
1, 2 and 3 ie. the newly imported users?

You don’t. That is a serialized array which is a PHP construct. MySQL has no idea what to do with it. To the database, it is just an string. To PHP it is a representation– a diagram– of an array and PHP takes it seriously. If you get a single character wrong in will not unserialize. If you were just copying a fixed string you might be able to pull this off with SQL but you are not– “I can also see that there are many values entered into each user_id”. To “insert” date into those strings you would have to reproduce PHP serialize in SQL.

Let the Core do the work for you:

$args = array(
  'exclude' => array(1,2,3),
);
$users = get_users($args);
foreach ($users as $u) {
  $u->set_role('subscriber');
}

Leave a Comment