Skip to content

Instantly share code, notes, and snippets.

@TimBHowe
Created April 29, 2020 19:29
Show Gist options
  • Save TimBHowe/99fe9330d02b895a6f7cd0f4921f0b9c to your computer and use it in GitHub Desktop.
Save TimBHowe/99fe9330d02b895a6f7cd0f4921f0b9c to your computer and use it in GitHub Desktop.
Update a WordPress user account first_name and last_name if it is missing with their WooCommerce billing_first_name and billing_last_name if available.
/* Get all the users missing a first_name. */
SELECT * FROM wp_usermeta AS A
WHERE A.meta_key = 'first_name'
AND A.meta_value = '';
/* Updated to first_name to their billing_first_name if available. */
UPDATE wp_usermeta AS A
JOIN ( SELECT user_id, meta_value FROM `wp_usermeta` WHERE wp_usermeta.meta_key = 'billing_first_name' AND wp_usermeta.meta_value <> '') AS B
ON A.user_id = B.user_id
SET A.meta_value = B.meta_value
WHERE A.meta_key = 'first_name'
AND A.meta_value = '';
/* Get all the users missing a last_name. */
SELECT * FROM wp_usermeta AS A
WHERE A.meta_key = 'last_name'
AND A.meta_value = '';
/* Updated to last_name to their billing_last_name if available. */
UPDATE wp_usermeta AS A
JOIN ( SELECT user_id, meta_value FROM `wp_usermeta` WHERE wp_usermeta.meta_key = 'billing_last_name' AND wp_usermeta.meta_value <> '') AS B
ON A.user_id = B.user_id
SET A.meta_value = B.meta_value
WHERE A.meta_key = 'last_name'
AND A.meta_value = '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment