【问题标题】:insert post ID with wp_insert_post使用 wp_insert_post 插入帖子 ID
【发布时间】:2011-09-27 08:21:06
【问题描述】:

如何选择帖子 ID,插入新帖子时,例如:

$post = array(
'ID'                =>  3333,
'comment_status'            =>  'open',
'post_content'      =>  'hi world!',
'post_name'         =>  'title_1',
'post_status'       =>  'publish',
'post_title'        =>  'sdfsfd fdsfds ds',
'post_type'         =>  'post',
);  

$post_id = wp_insert_post($post);

想要插入 id = 3333 的新帖子

【问题讨论】:

  • id 是数据库中的自动增量主字段。我不认为你能做到这一点。你到底为什么要这样做?
  • 我也有这种情况是必要的。我正在从另一个站点迁移数千个自定义帖子,每个帖子都有分类和元数据。通过将帖子 ID 设置为与旧站点相同,可以更轻松地导入其余数据。正如下面@daveaspinall 所建议的那样,我能够成功使用import_id
  • 对于未来的读者,请在接受的答案下方滚动,因为下面有一个非常有用的答案。

标签: wordpress plugins codex


【解决方案1】:

您可能想知道您可以使用'import_id' 而不是'ID',它会“尝试”并使用它。

在此处查看第二个示例:http://codex.wordpress.org/Function_Reference/wp_insert_post#Example

【讨论】:

    【解决方案2】:

    这是我的简单解决方案:

    //check if post with id 3333 is already in database, if so, update post 3333
    if (get_post_status(3333) ) {
        $post = array(
        'ID'                =>  3333,
        'comment_status'    =>  'open',
        'post_content'      =>  'hi world!',
        'post_name'         =>  'title_1',
        'post_status'       =>  'publish',
        'post_title'        =>  'your title',
        'post_type'         =>  'post',
        );  
    
        $post_id = wp_insert_post($post);
    }
    //if not in database, add post with id 3333
    else {
        $post = array(
        'import_id'         =>  3333,
        'comment_status'    =>  'open',
        'post_content'      =>  'hi world!',
        'post_name'         =>  'title_1',
        'post_status'       =>  'publish',
        'post_title'        =>  'your title',
        'post_type'         =>  'post',
        );  
    
        $post_id = wp_insert_post($post);
    }
    

    'ID'=> post_id 将更新该帖子,而 'import_id'=> post_id 将使用该 ID 创建一个新帖子。

    您还可以循环并提供 ID 以运行多个插入/更新,而不会冒创建无限数量的新帖子的风险。

    【讨论】:

    • 感谢您告知“import_id”
    【解决方案3】:

    对不起,伙计,不可行。以下是开发人员在法典上所说的话:

    重要提示:为 $post['ID'] 设置值不会创建具有该 ID 号的帖子。设置此值将导致函数使用 $post 中指定的其他值更新具有该 ID 号的帖子。简而言之,要插入新帖子,$post['ID'] 必须为空或根本不设置。

    http://codex.wordpress.org/Function_Reference/wp_insert_post

    【讨论】:

    【解决方案4】:

    正如 daveaspinall 所说。 我做了一个功能。

    require( 'wp-load.php' );
    function simpleImportPost($title,$import_id,$content){
    // Create post object
    $my_post = array();
    $my_post['post_title'] = $title;
    $my_post['import_id']=$import_id;
    $mypost['comment_status'] = 'closed';//I'll set all closed
    $my_post['post_content'] = $content;
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = 1;
    $my_post['post_category'] = array(0);
    // Insert the post into the database
    return wp_insert_post( $my_post );
    }
    

    示例:

    simpleImportPost('My Post 35',35,"35 Content");
    

    【讨论】:

      【解决方案5】:

      可以做到这一点,只是不能使用 API 的插入函数。您可以编写自己的 INSERT 查询。您总是希望尽可能使用 API,但有时这是不可能的。查询如下所示:

      global $wpdb;
      $wpdb->query( $wpdb->prepare("
          INSERT INTO {$wpdb->posts}
          VALUES( %d, %d, NOW(), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), %s, %s, %d, %s, %d, %s, %s, %d )",
          $ID,
          $post_author,
          $post_date_gmt,
          $post_content,
          $post_title,
          $post_excerpt,
          $post_status,
          $comment_status,
          $ping_status,
          $post_password,
          $post_name,
          $to_ping,
          $pinged,
          $post_modified_gmt,
          $post_content_filtered,
          $post_parent,
          $guid,
          $menu_order,
          $post_type,
          $post_mime_type,
          $comment_count
      ) );
      

      您必须首先确保该 ID 不存在于数据库中。如果将来发布表架构发生更改,您可能需要更新查询以考虑更改。

      【讨论】:

        猜你喜欢
        • 2020-10-22
        • 1970-01-01
        • 2014-06-30
        • 2015-11-04
        • 2021-04-08
        • 1970-01-01
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        相关资源
        最近更新 更多