Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced custom fields #7

Open
defunctl opened this issue May 8, 2013 · 7 comments
Open

Advanced custom fields #7

defunctl opened this issue May 8, 2013 · 7 comments

Comments

@defunctl
Copy link

defunctl commented May 8, 2013

Is there some way to get this to work with Advanced custom fields?

http://www.advancedcustomfields.com/

@unwrittendevin
Copy link

I've gotten it to work with using Advanced Custom Fields

    // Adding Advanced Custom Field Data
    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.',   SUV_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.', SUV_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;

    // 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 );
        }
        $taxonomies = get_object_taxonomies( $new_version['post_type'] );
            foreach( $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $this->original->ID, $taxonomy, array('fields' => 'names') );
            wp_set_object_terms( $this->post->ID, $terms, $taxonomy );
        }
        $custom_fields = get_post_custom( $this->original->ID );
            foreach ( $custom_fields as $key => $value ) {
            add_post_meta( $this->post->ID, $key, maybe_unserialize($value[0]) );
        }
    }
    update_post_meta( $this->post->ID, '_bu_version_copied_keys', $meta_keys);
}

@defunctl
Copy link
Author

Where did you add this code?

@defunctl
Copy link
Author

Ahh I see you've manually edited bu-versions.php. Is it possible for the authors to fork this into the core?

@unwrittendevin
Copy link

Sounds good to me. I have no issues with you guys adjusting my code to work
better with the overall plugin!

On Wed, Jan 22, 2014 at 5:22 PM, defunctl notifications@github.com wrote:

Ahh I see you've manually edited bu-versions.php. Is it possible for the
authors to fork this into the core?


Reply to this email directly or view it on GitHubhttps://github.com//issues/7#issuecomment-33075565
.

@mgburns
Copy link
Contributor

mgburns commented Jan 27, 2014

That particular approach to handling cloned meta data isn't something we're ready to merge in to this plugin, but we are actively investigating alternatives.

@ChongoShaun
Copy link

When previewing the page using the ACF code above, the changes made are not shown in preview mode. Just an FYI

@electricbrick
Copy link

Has any progress been made with custom post meta copying?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants