Php 同时使用wp_insert_post()和wp_insert_attachment()
我正在将1000多篇博客文章从一个旧的自定义数据库网站迁移到wordpress。在用php数组格式化每篇文章之后,我正在测试如何将它们迁移到wordpress 我想做的是迁移带有特色图片的博客文章。经过一些研究,我使用了wp_insert_post(),它只处理文本内容>,但它不插入特色图像,所以我还需要使用wp_insert_attachment()。问题是,当我在foreach循环中同时使用wp_insert_post()和wp_insert_attachment()时,它不能很好地工作。下面是我的代码。如果有人对这个问题有了解/方法,请给我建议。多谢各位Php 同时使用wp_insert_post()和wp_insert_attachment(),php,wordpress,Php,Wordpress,我正在将1000多篇博客文章从一个旧的自定义数据库网站迁移到wordpress。在用php数组格式化每篇文章之后,我正在测试如何将它们迁移到wordpress 我想做的是迁移带有特色图片的博客文章。经过一些研究,我使用了wp_insert_post(),它只处理文本内容>,但它不插入特色图像,所以我还需要使用wp_insert_attachment()。问题是,当我在foreach循环中同时使用wp_insert_post()和wp_insert_attachment()时,它不能很好地工作。下
$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 */
}
}
使用wp_insert_post()时不要添加ID,这将尝试使用该ID更新条目 重要提示:为$post['ID']设置值将不会创建具有 那个身份证号码。设置此值将导致函数更新 具有该ID号和$post中指定的其他值的post。 简而言之,要插入新帖子,$post['ID']必须为空或未设置 一点也不 在继续脚本之前,还要确保文章已经插入。乙二醇
$post_id = wp_insert_post( $my_post );
if(!$post_id) {
//log an error or something...
continue;
}
使用wp_insert_post()时不要添加ID,这将尝试使用该ID更新条目 重要提示:为$post['ID']设置值将不会创建具有 那个身份证号码。设置此值将导致函数更新 具有该ID号和$post中指定的其他值的post。 简而言之,要插入新帖子,$post['ID']必须为空或未设置 一点也不 在继续脚本之前,还要确保文章已经插入。乙二醇
$post_id = wp_insert_post( $my_post );
if(!$post_id) {
//log an error or something...
continue;
}
我还没有测试过,但我想这个可以
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 );
}
我还没有测试过,但我想这个可以
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 );
}
谢谢你的建议。我不会使用$post['ID']。我担心它会覆盖旧的帖子。wp_insert_post()不应该覆盖任何帖子,它应该只插入一个带有自动递增ID的新帖子。希望这对您有所帮助,如果是这样,请将我的答案标记为正确,谢谢。我明白了。谢谢但不幸的是,这并不能解决我的问题。我想知道我的代码的哪一部分是错误的,在迁移带有特色图片的帖子方面。然后你将不得不进行大量调试。。。例如,$parent_post_id分配到哪里?谢谢您的建议。我不会使用$post['ID']。我担心它会覆盖旧的帖子。wp_insert_post()不应该覆盖任何帖子,它应该只插入一个带有自动递增ID的新帖子。希望这对您有所帮助,如果是这样,请将我的答案标记为正确,谢谢。我明白了。谢谢但不幸的是,这并不能解决我的问题。我想知道我的代码的哪一部分是错误的,在迁移带有特色图片的帖子方面。然后你将不得不进行大量调试。。。例如,$parent\u post\u id是在哪里分配的?如果您在这里没有得到太多响应,最好在Wordpress StackExchange上发布此消息:。谢谢您的建议!我会的。如果你在这里没有得到太多的回应,最好在Wordpress StackExchange上发布此消息:。谢谢你的建议!我会做的。