我正在用wp\\u insert\\u post()追溯性地创建一篇文章,并在上面附加一幅特色图片。我需要将图像放入与帖子发布日期相对应的文件夹中。
我在wp\\u handle\\u upload函数中使用了时间参数,它应该根据参数值将文件移动到目录中(请参见代码中的-$upload\\u date-static value for now)。但是现在,图像移动到当前的年/月文件夹,忽略$upload\\u date(例如:应该去2007/11, 转到2012/12).
$output_dir = $uploads[\'path\'];
new_filepath = $output_dir.\'/\'.$file;
// INSERT POST AND GET POST ID
$my_post_id = wp_insert_post($my_post);
// Copy image from my folder to WP Uploads
rename($filename,$new_filepath);
// required for wp_handle_upload() to upload the file
$upload_overrides = array( \'test_form\' => FALSE );
$upload_date = \'2007/11\'; // Using this date for testing purposes
$movefile = wp_handle_upload( $new_filepath, $upload_overrides, $upload_date );
$wp_filetype = wp_check_filetype(basename($new_filepath), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => $post_title,
\'post_content\' => $post_content,
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $new_filepath, $my_post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $new_filepath );
wp_update_attachment_metadata( $attach_id, $attach_data );
// ADD FEATURED IMAGE TO THE POST
add_post_meta($my_post_id, \'_thumbnail_id\', $attach_id);