0

I would like to copy the meta_value from row to another (in the same table) where the user_id matches.

Each user in the wp_usermeta table has a meta_key/meta_value pair for first_name and a meta_key/meta_value pair for shipping_first_name. I would like to copy the shipping_first_name value to the first_name value where the user_id matches. I would like to update all users in the database at once (e.g., via phpMyAdmin).

How should this MySQL query be written?

1 Answer 1

1

use this code, it may help you

 $users = get_users($args);
      if($users) {
        foreach($users as $user) {
            $user_id = $user->ID;
            $shipping_first_name = get_user_meta($user_id, 'shipping_first_name', true);
                if($shipping_first_name) {
                  update_user_meta( $user_id, 'first_name', $shipping_first_name);  
                }
            }
        }

for $args - refer link https://codex.wordpress.org/Function_Reference/get_users

4
  • Thanks for your answer. I would like to execute a update query in order to update all users at once. Wouldn't you answer only update the currently logged-in user? Sorry that I was not more detailed with my question. I'll update it to be clearer. Commented Apr 21, 2018 at 9:36
  • I have updated code, please check if it will help :) Commented Apr 21, 2018 at 9:45
  • 1
    I used 'role' => 'Customer' for the $args and it worked perfectly. Thanks so much for your help! :) Commented Apr 21, 2018 at 9:54
  • Sounds Great it is working fine for you. Cheers :) Commented Apr 21, 2018 at 9:59

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.