Home Forums Backend Issues (wp-admin) Copy posts to user profiles


Copy posts to user profiles

  • Hi,

    I have 150 ‘user profiles’, saved as posts with a couple of custom fields.

    I’d like to move or copy all information to ‘real’ user profiles.

    I managed to re-create all fields and they show up on the profiles in de admin.

    Is there an easy way (anything but manual) to copy the information of the existing fields to the new (empty) fields)

  • Is there anything connecting the posts to the users? Is there some type of a relationship field that is populated?

    Is there some unique information that exists for both the post and the user?

    Without some way to know what post goes with what user the only way to do this would be manually.

    But it there is something that connects them you could create a temporary function that will do a query to get all of one (posts or users) then loop over them and then find an update the other end for each.

  • I think I’ve never populated custom fields programmatically before, sounds like a good idea. If that’s what you mean. Could you give a link to some information to help me started?


  • Unfortunately, there really isn’t a lot of information.

    Basically you need to create a temporary function, what I mean by that is that you create a function that you will remove after your run it once. I usually do this kind of thing on the “init” hook. Also, I do not update anything until I’m sure it’s going to run correctly.

    // add to functions.php
    add_action('init', 'migrate_user_data');
    function migrate_user_data() {
      // I usually set this to only run when I am logged it
      // so that this does not affect the site
      // swap id for your id
      if (!is_user_logged_in() || get_current_user_id() != 1) {
        // not me

    The rest of the function gets complicated and I don’t have specific code. I’ve pretty much explained already what you’ll do in this function.
    1) Do a query to get all of the posts of your post type
    2) Loop over the returned posts
    3) Get the user ID that matches the post by matching something in the post to the user
    4) get the ACF fields that you want to copy from the post
    5) Use update_field() to update the values for the user.Be sure to use the field key and not the field name for each field. Also supply the correct user ID for acf which will be 'user_'.$user_id. update_field('your-field-key, $value, 'user_'.$user_id);

  • I’m running a copy of the site in a test environment, so nothing can go (totally) wrong. Thanks for the head up, John!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.