Each of the 7,000+ users on my site has around 25 keys in the usermeta table. I won't list them all, but two of the keys are "access_code" and "association".

There are several dozen unique access codes for my site and each user is assigned only one access code.

Here's what I'm trying to do: I want to find all the users with the access_code key with value "abc" and for each of those users where that value is true, their association key would change to "XYZ Corporation".

Any thoughts?

1 Answer 1


You would need to use a meta query for this.

$meta_query_args = array(
        'key'     => 'access_code',
        'value'   => 'abc',
        'compare' => '='

$args =  array(
  'meta_query'   => $meta_query_args

$users = get_users( $args );

// User Loop
foreach ( $users as $user ) {
  update_user_meta( $user->ID, 'association', 'XYZ Corporation');
  • Thank you, Pim! I'll definitely give this a shot. Here comes a dumb question first. Where does this code go? It doesn't look like I'm supposed to put it into the SQL tab in phpmyadmin.... How do I execute this? Commented Oct 22, 2018 at 21:47
  • You can put it at the end of functions.php inside your theme folder. Make sure you back up your DB beforehand!
    – Pim
    Commented Oct 22, 2018 at 21:52
  • Will do and will let you know how it worked. Thanks again! Commented Oct 22, 2018 at 22:10
  • Worked like a charm! Thank you again, Pim!!! Here's another question for you. Can I copy/paste this query into my functions.php multiple times (once for each access code) so it automatically enters the association for each future user? I would think yes, but you seem to know what you're doing so I thought I'd ask before I do it. Commented Oct 23, 2018 at 6:05
  • Yes you can, you can also change this one. And you should remove it from your functions.phponce it's done, otherwise it will be run each time you visit a page on your site. Please consider upvoting/marking the answer as accepted if it helped.
    – Pim
    Commented Oct 23, 2018 at 6:58

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.