Migrating data from custom CMS to WordPress blog (2 posts)

  1. urkekg
    Posted 6 years ago #

    I'm writting PHP script to convert custom CMS data to WordPress. All going perfect, except images attached to posts. Here is steps my script do to attach images to posts:

    step 1 - insert new record in wp_posts table with this data:

    • ID - auto increment
    • post_author - 1 (admin)
    • post_date, post_date_gmt, post_modified and post_modified_gmt - same string in format YYYY-MM-DD HH:MM:SS
    • post_content - empty
    • post_title - same as post_title from original post
    • post_excerpt - empty
    • post_status - inherit
    • comment_status and ping_status - open
    • post_password - empty
    • post_name - image filename w/o extension
    • to_ping and pinged - empty
    • post_content_filtered - empty
    • post_parent - ID of original post
    • guid - full URL to image
    • menu_order - 0
    • post_type - attachment
    • post_mime_type - image/jpeg
    • comment_count - 0

    step 2 - insert two new records to wp_postmeta table

    • meta_id - auto increment
    • post_id - ID of original post to which I attaching image
    • meta_key - _wp_attached_file
    • meta_value - YYYY/MM/image_filename_with_extension


    • meta_id - auto increment
    • post_id - ID of original post to which I attaching image
    • meta_key - _wp_attachment_metadata
    • meta_value - a:6:{s:5:"width";s:3:"250";s:6:"height";s:3:"165";s:14:"hwstring_small";s:23:"height='85' width='128'";s:4:"file";s:63:"/var/www/vhost/devel/wp-content/uploads/2005/02/picture3.jpg";s:5:"sizes";a:1:{s:9:"thumbnail";a:3:{s:4:"file";s:20:"picture3-150x150.jpg";s:5:"width";s:3:"150";s:6:"height";s:3:"150";}}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}}

    step 3 - insert new record to wp_term_relationships table

    • object_id - image ID from wp_posts table
    • term_taxonomy_id - 9 (ID for taxonomy w/o description in wp_term_taxonomy table, same ID is assigned to new manualy attached images)
    • term_order - 0

    After this three steps, when I'm open Media -> Library in WP admin, for images automaticaly inserted with this script missing file type (JPG for other images) and thumbnail. If I try to Edit image, WP says Image data does not exist. Please re-upload the image. but if I cpopy&paste URL from File URL textbox to browser, I get image as I expect.

    Image files are in expected path on disk, with proper permissions.

    Where I'm wrong? What I'm missing to insert in WP base?


  2. urkekg
    Posted 6 years ago #

    OK, I figured it out.

    post_id in wp_postmeta table need to be ID of image record from wp_posts table, not ID of post to which image is attached.


Topic Closed

This topic has been closed to new replies.

About this Topic