Support

Account

Home Forums General Issues Clone a document and transfer ACF data

Solved

Clone a document and transfer ACF data

  • I’m writing some code to copy posts from one site in WordPress multisite to another. I’m basing it on the code found here. It uses get_post_custom to retrieve meta fields and add_post_meta to apply them to the new post.

    It doesn’t appear that I’m able to get data from the Flexible Layout field. Is that a thing that’s possible? Can you point me to a code snippet?

  • I figured out a decent solution using get_field_object and update_field.

    For anyone passing this way, you want to pass the value of the [‘value’] index of the retrieved object to update_field.

    $layout = get_field_object('field_name', $post_id);
    ...
    update_field('field_name', $layout['value'], $inserted_post_id);

    I’d love to have code that can iterate through fields w/o my having to hard-code the field name, though.

  • The problem with the code is that it assumes that any custom field that is an array value is a standard WP custom meta field with multiple db entries.

    
    foreach ($data as $key => $values) {
      // if you do not want weird redirects
      if($key == '_wp_old_slug') {
        continue;
      }
      foreach ($values as $value) {
        add_post_meta($inserted_post_id, $key, $value);
      }
    }
    

    ACF stores a lot of fields as serialized arrays, including information about the rows of a flex field. This code will break any ACF fields that expect an array to be stored.

    So you need to store standard WP fields the way that this is doing and you need to store ACF fields as the array that is originally returned.

    The following is a guess and I can’t guarantee it will work, it may still break something.

    
    // add before loop metadat loop
    restore_current_blog();
    
    foreach ($data as $key => $values) {
      // if you do not want weird redirects
      if($key == '_wp_old_slug') {
        continue;
      }
      
      // try to detect if this is an ACF field
      // this needs to be done on the original post
      $field = get_field_object($key, $post_id, false, false);
      
      // switch blog
      switch_to_blog($blog_id);
      
      if ($field) {
        // we think it's an acf field
        // preserve it in whatever format it is
        upate_post_meta($inserted_post_id, $key, $value);
      } else {
        // we think it's a regular WP field
        foreach ($values as $value) {
          add_post_meta( $inserted_post_id, $key, $value );
        }
      }
      // restore the blog so next loop can check the original post field
      restore_current_blog();
    }
    
  • That doesn’t seem to work. If I dump the $value variable before the line

    upate_post_meta($inserted_post_id, $key, $value);

    It’s the key of the field, not the field structure/array. But if I try to pass the $field as the last argument of update_post_meta(), it doesn’t work.

  • Did you ever get this to work? I need the exact same thing. I have a flexible content field I need to update on one site and push to another.

  • Not really. Most of my ACF data for that site was in a single field. I added code to copy that field specifically, like:

    
    $layout = get_field_object('content_rows', $post_id);
    ...
    update_field('content_rows', $layout['value'], $inserted_post_id); 
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.