Recover specific taxonomies relationship from DB backup in WordPress

You are entering the world of Sysadmin and SQL. Clean your shoes on the doormat before enter or you will hurt badly by the sysadmin.

I will tell you now how to recover from an old db copy the relationship but also all the data between a specific taxonomy and another one.

Let’s see the user case:

  • Bulk removal of different terms from a taxonomy
  • After a week someone notices that are missing few terms that should stay there

How you can fix that? In WordPress is dangerous recover only a part of the database and ignoring the rest because of a lot of relationship and serialized data.

One of the interesting things is that WordPress save the term data in 4 different tables, that start withwp_terms*

So let’s see the workflow:

  • Export from the old database with all the data only this 4 tables
  • Don’t include the create table... part
  • Change all the INSERT INTO in INSERT IGNORE INTO
  • Execute this altered dump in your MySQL instance

Now you will see that a lot of them are not executed because of the magic of the IGNORE method (included in the dump) that in case of a duplicate ID will not execute the INSERT.

In this simple way you can recover all the old relationships and data without breaking anything else.

The second step is to have a local copy of the DB (I didn’t mention before but I guess that you already have it to work freely).

In this local copy you can import the current db of the website, execute this altered dump and test if everything is working.

Later you can cleanup and removing all the taxonomies as example that you don’t want, with wpcli as example.

From this local copy you can do another dump with this 4 tables and do again this change of IGNORE command.

Now you have a ready specific set of changes to do on your db and everything will work.

Of course if you are doing that without any multiple copy of the DB in the various step is on your own!

Leave a Reply

Your email address will not be published. Required fields are marked *

Recover specific taxonomies relationship from DB backup in WordPress

time to read: 1 min
0