0

I'm working on a wordpress project that involves getting ~990 entries from a spreadsheet into the system. I've been able to create CSV files to create the posts, as well as their associations with a handful of custom taxonomies. Now however I have to figure out this media thing.

990 posts, almost all of which need to have an image and a video file associated with them.

I've tried a plugin for importing csv data into the database, and in wp_postmeta creating entries with the associated Post ID, a meta key of "_wp_attached_file", and the file name. I also added entries for each image into the posts table, including what SHOULD be the right guid, post_parent, and post_mime_type.

The theme I have in place can pull the data as it needs to and display as it should, but behind the scenes is where the problem lies. I go to edit a post and unless it's one where I manually added the media through editing the post itself, no image shows up. There's SOME association I must be missing in the database somewhere, but I haven't been able to track it down yet. There has to be a way to handle this through SQL, as opposed to manually editing every entry.

I'm looking at _wp_attachment_metadata, and I'm wondering if that's the key? Are the entries in wp_postmeta and wp_posts really essentially pointless, and it's all on _wp_attachment_metadata? I'd hate to set out on hacking together 990 (video too, so 1980) serialized entries only to find out that's still not the missing link.

I have the images in wp_posts with the proper parent, and in postmeta with the right information. Something is still missing though...

4

2 回答 2

0

wp_insert_attachment

$attach_id = wp_insert_attachment($attachment, $filename, $post->ID);
$attach_data = wp_generate_attachment_metadata($attach_id, $path . $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
于 2013-06-07T14:41:20.647 回答
0

以下是我从我编写的另一个插件中获取的方法。这用于上传的文件 - $file 是一个包含结果的 post 数组 - 但是,如果您在磁盘上的某个地方有文件,那么您可以将 $filename 设置为该文件(删除 move_uploaded_file)。

    $uploads = wp_upload_dir();

    if ($uploads['error'] != '')
        wp_die($uploads['error']);

    $target_dir = $uploads['path'];

    if (!file_exists ($target_dir))
        mkdir ($target_dir);

    $filename = $file['name'];

    // If you get script file, it's a danger. Make it TXT file.
    if ( preg_match( '/\.(php|pl|py|rb|cgi)\d?$/', $filename ) )
            $filename .= '.txt';

    $new_filename = wp_unique_filename( $target_dir, $filename );
    $new_url = trailingslashit( $uploads['url'] ).$new_filename;
    $new_filename = trailingslashit( $target_dir ) . $new_filename;

    $upload_result[$filename]['new_filename']=$new_filename;

    $upload_result[$filename]['valid'] = false;
    $upload_result[$filename]['reason'] = '';
    $upload_result[$filename]['url'] = $new_url;
    // Detect whether the uploaded file is an image

    $is_image = preg_match ('/(jpeg|png|gif)/i', $file['type']);
    $type = ($is_image) ? "img" : "file";

    if (!$is_image) {
        $upload_result[$filename]['reason'] =  "Sorry, you can only upload images.";
    } else if (move_uploaded_file ($file['tmp_name'], $new_filename)) {
        $upload_result[$filename]['valid'] = true;
        $upload_result[$filename]['reason'] = '';

        // Make sure the uploaded file is only readable for the owner process
        @chmod( $new_file, 0400 );
        //
        // now attach this to the original post - not really required except that it will ensure
        // that the image shows up on the right place in the media library and when viewing the post.
        $wp_filetype = wp_check_filetype(basename($new_filename), null );
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit'
            );
        $attach_id = wp_insert_attachment( $attachment, $new_filename, $commentData['comment_post_ID'] );

        // need to include the image.php file so  function wp_generate_attachment_metadata() works
        require_once(ABSPATH . 'wp-admin/includes/image.php');

        $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
        wp_update_attachment_metadata( $attach_id, $attach_data );
于 2013-06-07T14:46:53.757 回答