Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to revise an existing WP plugin BU Versions to accept different custom meta field values from items I've set-up using the Advanced Custom Fields (ACF) plugin.

BU Versions will copy over the title, excerpt, content and a few other page specific features from one page to another, but I can't get it to copy over the custom meta values.

For example, when I print_r($new_version), I get this in my results for what exists within the array, so I know it is adding the custom meta fields and values that are currently on the live page I am testing post duplication on.

Array(
[post_type] => page_alt
[post_parent] => 40
[ID] =>
[post_status] => draft
[post_content] =>
[post_name] => resources
[post_title] => Resources
[post_excerpt] => 
[_subheading] => For ELI Students
[_edit_last] => 3
[_wp_page_template] => default
[_edit_lock] => 1368111234:3
[add_columns] => 0
[_add_columns] => field_37
[left_column] => Array()
[_left_column] => field_39
[right_column] => Array()
[_right_column] => field_61
[header_image] => 426
[_header_image] => field_64
[_yoast_wpseo_linkdex] => 0
[3] =>
)

And this is the part of the code where I think the plugin creates a new post based on existing post values

/**
 * Create a new alternate version.
 *
 * @param mixed $post
 * @param mixed $alt_post_type
 * @access public
 * @return int|WP_Error
 */
function create( $post_id, $alt_post_type, $meta_keys = null ) {
    $this->get_version( $post_id );
    if ( $this->has_version() ) {
        return new WP_Error( 'alternate_already_exists', __( 'An alternate version already exists for this post.', BUV_TEXTDOMAIN ) );
    }
    $this->original = get_post( $post_id );
    if ( ! isset( $this->original ) ) {
        return new WP_Error( 'alternate_no_original', sprintf( __( 'The post ID: %s could not be found.', BUV_TEXTDOMAIN ), $post_id ) );
    }
    $new_version['post_type'] = $alt_post_type;
    $new_version['post_parent'] = $this->original->ID;
    $new_version['ID'] = null;
    $new_version['post_status'] = 'draft';
    $new_version['post_content'] = $this->original->post_content;
    $new_version['post_name'] = $this->original->post_name;
    $new_version['post_title'] = $this->original->post_title;
    $new_version['post_excerpt'] = $this->original->post_excerpt;
    $new_version['_subheading'] = $this->original->_subheading;
    // Loop through Post Custom Meta and add to array 
    $custom_field_keys = get_post_custom_keys($new_version['post_parent']);
    foreach ( $custom_field_keys as $key => $value ) {
        $new_version[$value] = $this->original->$value;
    }
    //
    $result = wp_insert_post($new_version);
    if ( ! is_wp_error( $result ) ) {
        $this->post = get_post( $result );
        $this->copy_original_meta( $new_version );
        update_post_meta( $this->original->ID, self::tracking_meta_key, $this->post->ID );
    }
    return $result;
}
/* Because of sanization and serialization, it may be better to use SQL, but for now we are using the API
 **/
private function copy_original_meta( $new_version ) {   
    foreach ( $new_version as $key ) {
        $values = get_post_meta( $this->original->ID, $key );
        foreach ( $values as $v ) { 
            update_post_meta( $this->post->ID, $key, $v );
        }
    }
    update_post_meta( $this->post->ID, '_bu_version_copied_keys', $meta_keys);
}
/**

How can I get per say "Header Image" which is a ACF image field with a value of "field_64" on my page to populate the image field with the image ID of "426" when I duplicate the post? Or

<div id="acf-header_image" class="field field_type-image field_key-field_64" data-field_name="header_image" data-field_key="field_64" data-field_type="image">
  <p class="label">
    <label for="acf-field-header_image">Header Image</label>
    Choose the image that will display in the header. If none is chosen, then a default image will be displayed. <em style="color:red;">Make sure image chosen has dimensions that meet or exceed 1170 pixels across.</em>
  </p>
  <div class="acf-image-uploader clearfix " data-preview_size="banner">
    <input class="acf-image-value" type="hidden" name="fields[field_64]" value="">
      <div class="has-image">
        <div class="hover">
          <ul class="bl">
            <li><a class="acf-button-delete ir" href="#">Remove</a></li>
            <li><a class="acf-button-edit ir" href="#">Edit</a></li>
          </ul>
        </div>
        <img src="" alt="">
      </div>
    <div class="no-image">
      <p>No image selected <input type="button" class="button add-image" value="Add Image"></p>
    </div>
  </div>
</div>
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.