I need to import an old database into WordPress. Easy right? Well not that easy, I need to keep the same post id because I use a custom permalink structure that includes the post id.
For example : /%postname%+id-%post_id%.html.
How can I do this?
find out your oldest ( and smallest id ) in your old database. let's assume that's 45.
start with a fresh install of WP and delete the "hello world" post so the post,post_meta and term_relationships tables do not have anything to do with that "hello world" post
truncate your posts table and set the starting value to 45 so the first record entered will have ID 45.
start your migration, one record at a time
before migrate your old db record, calculate the difference between that and the prev record. for example, if your next post got the ID 46, then 46-45=1. 1 is good, so do the migration normally. ( run the wp_insert normally ) when you spot that the difference is not 1, then create as many dummy records as needed. for example, if the record is 49, then 49-45=4. This means, you need 3 dummy records. So run 3 wp_inserts with the phrase "to be deleted" in the post title. and then do your 49th record migration.
you keep going like this until you reach your biggest ( youngest ) record in your old.
then simply delete all recs with the title "to be deleted". but use the WP api's throughout. so that the relational integrity across tables are maintained for you.