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?
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?
You must be logged in to post a comment.
Assuming that these are both WordPress databases, then the easiest way to handle this is using
mysqldump
to create an export and then execute the resulting SQL file against your new database. By default it will exporta all tables, so if you just want some of them you will need to explicitly specify them. The “important” tables for WordPress posts:wp_posts
– the postswp_postmeta
– post meta valueswp_term_relationships
– posts to taxonomieswp_term_taxonomy
– taxonomieswp_terms
– actual tag and category valuesTo export your database to a SQL file, replacing USERNAME, PASSWORD, and DATABASE appropriately. This will drop/create/populate the tables, so if you just want data use the
âno-create-info
option.To then import the
dump.sql
file to your new database:All database IDs will be maintained.
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.