最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding Post Title from custom post type to another custom post type

programmeradmin2浏览0评论

I have different CPT and add post title and Feature Image but i want to add post title from one CPT to all CPT post title and feature image. Is this possible to add title and feature image from different CPT. im using code.

Example : CPT1 : Entertainment CPT2 : Movies CPT3 : Songs CPT4 : Videos

CPT1 : Entertainment

Click > Add new Post when I editing CPT1, I want to add title and feature image of CPT2, CPT3, CPT4 from CPT1.

I have different CPT and add post title and Feature Image but i want to add post title from one CPT to all CPT post title and feature image. Is this possible to add title and feature image from different CPT. im using code.

Example : CPT1 : Entertainment CPT2 : Movies CPT3 : Songs CPT4 : Videos

CPT1 : Entertainment

Click > Add new Post when I editing CPT1, I want to add title and feature image of CPT2, CPT3, CPT4 from CPT1.

Share Improve this question asked Jun 26, 2019 at 5:16 Samir MaharjanSamir Maharjan 1
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, you can do that, Please try below code. add in your functions.php please change post_types according your post type slugs from below code.

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

  }

let me know if this helps to you!

发布评论

评论列表(0)

  1. 暂无评论