如何将内容从一种帖子类型复制到另一种帖子类型?

时间:2018-03-06 03:01:48

标签: wordpress post

无论何时创建或编辑事件,我都会尝试将某些字段从事件自定义帖子类型复制到标准博客帖子。我现在已经开始工作,当一个事件被更新或编辑时,它将创建一个博客文章,但如果我要添加一个全新的事件,它会添加2个博客文章,然后添加正确的帖子与内容。它创建的第一个帖子是自动草稿,然后它创建一个标题和作者,但没有内容。它创建的第3个帖子是正确的。当我添加新活动时,如何防止它创建这些额外的帖子?

到目前为止,这是我的代码:

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' );

1 个答案:

答案 0 :(得分:0)

搞定了。如果有人想看看它是如何工作的,那么这是最终的代码。

grouped_items = zip(*[seq[i::3] for i in range(3)])