0

I am working on migrate blog posts, which are more than 1000 posts, from an old custom database website to wordpress. After formatted each post with php array, I am testing how to migrate them to wordpress.

What I want to do is migrate blog posts with featured images. After some research, I used wp_insert_post() which worked just with the text contents> However it does not insert featured images, so I need to use wp_insert_attachnment() as well. The problem is when I use wp_insert_post() and wp_insert_attachnment() together, inside foreach loop, it does not work well. The code I have is below. If anyone has knowledge/approach about this issue, please give me advice. Thank you.

$wp_posts = array(
    array(
        'post_id'        => '10',
        'post_title'     => 'Post Title 1',
        'post_date'      => '2015-06-22',
        'post_content'   => 'Post Content 1',
        'post_imagePath' => 'http://localhost/test/wp-content/uploads/2016/01/image1.jpg'
    ),
    array(
        'post_id'        => '11',
        'post_title'     => 'Post Title 2',
        'post_date'      => '2015-06-22',
        'post_content'   => 'Post Content 2',
        'post_imagePath' => 'http://localhost/test/wp-content/uploads/2016/01/image2.jpg'
    )
);


$filetype['type']   = 'image/jpeg';
$last = count($wp_posts) - 1;
foreach ($wp_posts as $i => $row){
    $ID                 = $row['post_id'];
    $title          = $row['post_title'];
    $content            = $row['post_content'];
    $postdate           = $row['post_date']. " 12:00:00";
    $imagePath      = $row['post_imagePath'];
    if (!get_page_by_title($title, 'OBJECT', 'post') ){

    $my_post = array(
                'ID'           => $ID,
                'post_title'   => $title,
                'post_content' => $content,
                'post_date'    => $postdate,
            );

     wp_insert_post( $my_post );

    /* wp_insert_attachment */
    $filetype = wp_check_filetype( basename( $imagePath ), null );
    $wp_upload_dir = wp_upload_dir();
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $imagePath ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $imagePath ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $imagePath, $parent_post_id );
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $imagePath );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    set_post_thumbnail( $parent_post_id, $attach_id );
    /* ./wp_insert_attachment */

    }
}
shinyatk
  • 855
  • 11
  • 28

2 Answers2

2

Do not add the ID when using wp_insert_post(), this will try and update an entry with that ID.

IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all.

wp_insert_post() documentation

Also make sure the post has actually been inserted before continuing your script. eg

$post_id = wp_insert_post( $my_post );
if(!$post_id) {
    //log an error or something...
    continue;
}
Lloyd Erasmus
  • 402
  • 10
  • 17
  • Thank you for your advice. I will not use $post['ID']. I'm afraid it would overwrite the old posts. – shinyatk Jan 14 '16 at 15:10
  • wp_insert_post() should not overwrite any posts, it should simply insert a new post with an auto incremented ID. Hope this helps you, if so please mark my answer as correct, thank you. – Lloyd Erasmus Jan 14 '16 at 15:16
  • I see. Thanks. But it does not solve my question unfortunately. I woule like to know which part of my code is wrong, in terms of migrating posts with featured images. – shinyatk Jan 14 '16 at 15:31
  • 1
    You will have to do quite a bit of debugging then... for instance where does $parent_post_id get assigned? – Lloyd Erasmus Jan 14 '16 at 16:12
1

Haven't test it but i think this will do it.

foreach ($wp_posts as $i => $row){
    $ID                 = $row['post_id'];
    $title          = $row['post_title'];
    $content            = $row['post_content'];
    $postdate           = $row['post_date']. " 12:00:00";
    $imagePath      = $row['post_imagePath'];
    
    $my_post = array(
                'ID'           => $ID,
                'post_title'   => $title,
                'post_content' => $content,
                'post_date'    => $postdate,
            );

    $parent_post_id = wp_insert_post( $my_post ); //Returns the post ID on success. 

    /**** wp_insert_attachment ****/
    
    $filetype = wp_check_filetype( basename( $imagePath ), null );
    $wp_upload_dir = wp_upload_dir();   
    
    $attachment = array(
        'post_mime_type' => $filetype['type'],
        'post_title' => sanitize_file_name(basename($image_url)),
        'post_content' => '',
        'post_status' => 'inherit'
    );

    // So here we attach image to its parent's post ID from above
    $attach_id = wp_insert_attachment( $attachment, $imagePath, $parent_post_id);
    // Attachment has its ID too "$attach_id"
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $imagePath );
    $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
    $res2= set_post_thumbnail( $parent_post_id, $attach_id );

}
Sufyan Shaik
  • 23
  • 1
  • 5