【问题标题】:Using wp_insert_post() and wp_insert_attachment() at the same time同时使用 wp_insert_post() 和 wp_insert_attachment()
【发布时间】:2016-01-14 14:59:37
【问题描述】:

我正在将 1000 多篇博文从旧的自定义数据库网站迁移到 wordpress。在使用 php 数组格式化每个帖子后,我正在测试如何将它们迁移到 wordpress。

我想做的是迁移带有特色图片的博客文章。经过一番研究,我使用了 wp_insert_post(),它只适用于文本内容>但是它不插入特色图像,所以我也需要使用 wp_insert_attachnment()。问题是当我在 foreach 循环中一起使用 wp_insert_post() 和 wp_insert_attachnment() 时,它不能正常工作。我的代码如下。如果有人对此问题有知识/方法,请给我建议。谢谢。

$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 */

    }
}

【问题讨论】:

  • 如果您在这里没有得到太多回应,最好将其发布在 Wordpress StackExchange 上:wordpress.stackexchange.com
  • 感谢您的建议!我会做的。

标签: php wordpress


【解决方案1】:

使用 wp_insert_post() 时不要添加 ID,这将尝试使用该 ID 更新条目。

重要提示:为 $post['ID'] 设置值不会创建带有 那个身份证号码。设置此值将导致函数更新 具有该 ID 号的帖子以及 $post 中指定的其他值。 简而言之,要插入新帖子,$post['ID'] 必须为空或未设置 完全没有。

wp_insert_post() documentation

在继续执行脚本之前,还要确保帖子已实际插入。例如

$post_id = wp_insert_post( $my_post );
if(!$post_id) {
    //log an error or something...
    continue;
}

【讨论】:

  • 感谢您的建议。我不会使用 $post['ID']。恐怕它会覆盖旧帖子。
  • wp_insert_post() 不应该覆盖任何帖子,它应该简单地插入一个具有自动递增 ID 的新帖子。希望对你有帮助,如果可以,请把我的回答标记为正确,谢谢。
  • 我明白了。谢谢。但不幸的是,它并没有解决我的问题。在迁移带有特色图片的帖子方面,我想知道我的代码的哪一部分是错误的。
  • 那么您将不得不进行大量调试...例如 $parent_post_id 分配到哪里?
【解决方案2】:

尚未测试,但我认为这可以做到。

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 );

}

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    相关资源
    最近更新 更多