Kang Yun
Kang Yun

Reputation: 71

how to copy content from one post type to another?

I am trying to copy certain fields from a events custom post type to a standard blog post whenever an event is created or edited. I now have it working to the point that when an event is updated or edited it will create a blog post but if I am adding a brand new event it adds 2 blog post before adding the correct post with content. The first post it creates is auto draft then it creates one with the title and author but no content. The 3rd post it creates is correct though. How can I prevent it from creating these extra post when I add a new event?

This is my code so far:

function create_event_post( $post_id ) {

    // If this is just a revision, don't create a new post.
    if ( wp_is_post_revision( $post_id ) )
        return;

    // Set the title, thumbnail id, author, and content variables
    $post_title = get_the_title( $post_id );
    $post_type = get_post_type($post_id);
    $post_content = get_post_field('post_content', $post_id);
    $thumbnail_id = get_post_thumbnail_id( $post_id );
    $author_id = get_post_field ('post_author', $post_id);
    $author_name = get_the_author_meta( 'display_name' , $author_id );  

    // Set the variables for the new post to the same as the event post     
    $new_post = array(
            'comment_status'    =>  'closed',
            'ping_status'       =>  'closed',
            'post_author'       =>  $author_id,
            'post_title'        =>  $post_title,
            'post_content'      =>  $post_content,
            'post_status'       =>  'publish',
            'post_type'     =>  'post'
        );  

    // If the post is not "tribe_events", don't create a new post.  
    if ( "tribe_events" != $post_type ) 
        return;

    // Create the new post and retrieve the id of the new post
    $new_post_id = wp_insert_post ( $new_post );
    // Set the featured image for the new post to the same image as event post 
    set_post_thumbnail( $new_post_id, $thumbnail_id );      

}
add_action( 'save_post', 'create_event_post' );

Upvotes: 0

Views: 663

Answers (1)

Kang Yun
Kang Yun

Reputation: 71

Got it working. Here is the final code if anyone wants to see how it works.

add_action( 'save_post', 'create_event_post' );

function create_event_post( $post_id ) {

    // Set the title, thumbnail id, author, and content variables
    $post_title = get_the_title( $post_id );
    $post_type = get_post_type($post_id);
    $post_content = get_post_field('post_content', $post_id);
    $thumbnail_id = get_post_thumbnail_id( $post_id );
    $author_id = get_post_field ('post_author', $post_id);

    // If the post is not "tribe_events", don't create a new post.  
    if ( "tribe_events" != $post_type ) 
        return;

    $new_post = array(
                'comment_status'    =>  'closed',
                'ping_status'       =>  'closed',
                'post_author'       =>  $author_id,
                'post_title'        =>  $post_title,
                'post_content'      =>  $post_content,
                'post_status'       =>  'publish',
                'post_type'     =>  'post'
            );

    remove_action( 'save_post', 'create_event_post' );

    $post_exists = get_page_by_title( $post_title, $output, "post" );

    if ( !empty($post_exists) ) {
        // Update post
        $update_post = array(
            'ID'           =>   $post_exists->ID,
            'post_title'   =>   $post_title,
            'post_content' =>   $post_content,
        );

        // Update the post into the database
        wp_update_post( $update_post );
        set_post_thumbnail( $post_exists->ID, $thumbnail_id );
    }
    else {
        // Create the new post and retrieve the id of the new post
        $new_post_id = wp_insert_post ( $new_post );
        // Set the featured image for the new post to the same image as event post 
        set_post_thumbnail( $new_post_id, $thumbnail_id );
    }           

    // Now hook the action
    add_action( 'save_post', 'create_event_post' );
}

Upvotes: 1

Related Questions