0

Strange situation, but a Wordpress site i'm building has 1200 users already imported via csv, and they all have posts associated with them. I now need to add a number of extra fields to each user, but the only way I know how to is mass delete all users, and re-upload the new CSV with all the extra fields(and with the current USERID to match existing).

My question, is how can I do this and keep all posts associated to a user? Has anyone done this before? Will it totally break the website?

Is there a workaround that I havn't mentioned here??

1 Answer 1

0

You don't need to re-import the CSV to add metas. Assuming you want to do this for every user on the site here is what you do.

$users = get_users(); // this will get all users on your site.

// loop through each user updating their meta
foreach( $users as $user ) {
     // whatever key/value pair you want to add to each user
     // you can have as many as you want
     update_user_meta( $user->ID, $meta_key, $meta_value );
}

Put this in your theme's functions.php file and load your site ONCE. Because this temporary code will execute on each page load so just load it once and remove this code. This will successfully add the meta you want to each user.

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.