How can I assign post a specific ID on creation?

I’m migrating content from an existing site into wordpress.
The old site has cross links that I’d like to maintain in the new site.
I can convert the old links to wordpress format links (based on post-id), but in order to do that I need to assign the posts their own ID’s on migration.
is there any way to do that ?

The old site is NOT wordpress based…

Related posts

Leave a Reply

2 comments

  1. Yes, use the “import_id” field in the post, when calling wp_insert_post.

    This is treated as a “suggested” ID for the post that will be used if no post with that ID already exists.

    $post = array(
    'post_title'=>'whatever',
    'post_content'=>'whatever',
    'import_id'=>123
    );
    wp_insert_post($post);
    
  2. Do you mean that old site used “ugly” permalinks? http://example.com/?p=N

    I am not entirely sure about mechanics of import but in general case WordPress wp_insert_post() function does not allow to create posts with specific ID. Such input is treated as attempt to update existing post and fails (I assume from look at code, not tested) if post does not exists. See Otto’s answer about import_id which I missed. Following still stands for cases when IDs are not numeric or otherwise can’t be smoothly transferred to WP IDs.

    My idea would be to map old ids to guid in WP, then run replacements for links in database.