将帖子标题从自定义帖子类型添加到另一自定义帖子类型

时间:2019-06-26 作者:Samir Maharjan

我有不同的CPT和添加文章标题和功能图像,但我想将一个CPT中的文章标题添加到所有CPT的文章标题和功能图像中。这是否可以添加来自不同CPT的标题和特征图像。im使用代码。

示例:CPT1:娱乐CPT2:电影CPT3:歌曲CPT4:视频

CPT1:娱乐

编辑CPT1时,单击>添加新帖子,我想从CPT1中添加CPT2、CPT3、CPT4的标题和特征图像。

1 个回复
SO网友:Chetan Vaghela

是的,您可以这样做,请尝试下面的代码。添加您的函数。php请根据下面代码中的post-type-slug更改post\\u类型。

add_action( \'save_post\',  \'post_entertainment_save\' ) ;


function post_entertainment_save( $post_id ) {

    $post_type = get_post_type($post_id);
    $post_title = get_the_title( $post_id );
    $post_thumbnail_id = get_post_thumbnail_id($post_id);
    $imageSRC = wp_get_attachment_image_src($post_thumbnail_id, \'thumbnail\');
    $post_content = get_post_field(\'post_content\', $post_id);

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

    remove_action( \'save_post\', \'post_entertainment_save\' );

    $movies_post_exists = get_page_by_title( $post_title, $output, "movies" );

    if ( !empty($movies_post_exists) ) {
        // Update post
        $movie_update_post = array(
            \'ID\'           =>   $movies_post_exists->ID,
            \'post_title\'   =>   $post_title,
            \'post_content\' =>   $post_content,
            \'post_type\'=>\'movies\', 
        );

        // Update the post into the database
        wp_update_post( $movie_update_post );
        set_post_thumbnail( $movies_post_exists->ID, $post_thumbnail_id );
    }
    else {
        $movie_post = array(
          \'post_title\'=>$post_title, 
          \'post_type\'=>\'movies\', 
          \'post_content\'=>$post_content,
          \'post_status\' =>  \'publish\',
        );
        // Create the new post and retrieve the id of the new post
        $movie_post_id = wp_insert_post ( $movie_post );
        // Set the featured image for the new post to the same image as event post 
        set_post_thumbnail( $movie_post_id, $post_thumbnail_id );
    }   

    $song_post_exists = get_page_by_title( $post_title, $output, "songs" );

    if ( !empty($song_post_exists) ) {
        // Update post
        $songs_update_post = array(
            \'ID\'           =>   $song_post_exists->ID,
            \'post_title\'   =>   $post_title,
            \'post_content\' =>   $post_content,
            \'post_type\'=>\'songs\', 
        );

        // Update the post into the database
        wp_update_post( $songs_update_post );
        set_post_thumbnail( $song_post_exists->ID, $post_thumbnail_id );
    }
    else {
        $song_post = array(
          \'post_title\'=>$post_title, 
          \'post_type\'=>\'songs\', 
          \'post_content\'=>$post_content,
          \'post_status\' =>  \'publish\',
        );
        // Create the new post and retrieve the id of the new post
        $song_post_id = wp_insert_post ( $song_post );
        // Set the featured image for the new post to the same image as event post 
        set_post_thumbnail( $song_post_id, $post_thumbnail_id );
    }  


     $video_post_exists = get_page_by_title( $post_title, $output, "videos" );

    if ( !empty($video_post_exists) ) {
        // Update post
        $videos_update_post = array(
            \'ID\'           =>   $video_post_exists->ID,
            \'post_title\'   =>   $post_title,
            \'post_content\' =>   $post_content,
            \'post_type\'=>\'videos\', 
        );

        // Update the post into the database
        wp_update_post( $videos_update_post );
        set_post_thumbnail( $video_post_exists->ID, $post_thumbnail_id );
    }
    else {
        $video_post = array(
          \'post_title\'=>$post_title, 
          \'post_type\'=>\'videos\', 
          \'post_content\'=>$post_content,
          \'post_status\' =>  \'publish\',
        );
        // Create the new post and retrieve the id of the new post
        $video_post_id = wp_insert_post ( $video_post );
        // Set the featured image for the new post to the same image as event post 
        set_post_thumbnail( $video_post_id, $post_thumbnail_id );
    }        

    // Now hook the action
    add_action( \'save_post\', \'post_entertainment_save\' );

  }
如果这对你有帮助,请告诉我!

相关推荐