• Welcome to the all-new Kolakube support, the official place to get help for Marketers Delight, XFtoWP, CryptoWP, and other WordPress products! Please login with your account details to access private support forums.

Changing the name of a custom post type or taxonomy without losing everything

Alex

MD developer
Staff member
Md
XFtoWP
Messages
6,546
Reaction score
1,847
Let's go deep—

Say you run into a situation where you need to change the name of a post type in your database, how do you do it?

Changing the post type/taxonomy name in register_post_type() or register_taxonomy() will cause all of your existing content to disappear since technically by changing the name, we are creating an old post type.

I just ran into this situation in a project so I figured I would document it here as an exercise. In case you didn't know, another word for Taxonomies is Categories.

Part 1: Here is the taxonomy I am working with. It was incorrectly named so for organization sake, it will be best to rename it. I don't want to lose existing content so let's see how we can preserve them after I rename the taxonomy in register_taxonomy().

Screen Shot 2019-06-27 at 5.49.54 PM.png

Part 2: Just as suspected, everything is gone after changing the name! I can stay calm though because I know my content is still somewhere in the database. So let's go there.

Screen Shot 2019-06-27 at 5.53.09 PM.png

Part 3: Logging into the web host/server, go to phpMyAdmin for my site's database and run an SQL query. I've used the queries from this Stack Overflow thread before so this will be a breeze. Even if working with the site's database is intimidating, this level of access is a major benefit of why we choose to work with WordPress, so we ought to take advantage of it for maintenance like this.

I will fill in the old and new name across the entire database with this SQL query. All this does is search through the names of all of the taxonomies registered on the site and changes the old name with the new name, thus restoring content to the new name.

Screen Shot 2019-06-27 at 5.56.26 PM.png

Rename post type:

UPDATE `wp_posts` SET `post_type` = '<new post type name>' WHERE `post_type` = '<old post type name>';


Rename taxonomy:

UPDATE `wp_term_taxonomy` SET `taxonomy` = '<new taxonomy name>' WHERE `taxonomy` = '<old taxonomy name>';


Part 4: After getting the query right, you can usually run a simulation to be sure you are changing exactly what you want to change (you have to be very present here!). The results sent back 11 matches for changing the old name to the new name in the database, so I am going to execute the query.

Screen Shot 2019-06-27 at 6.07.04 PM.png

Part 5: The big reveal! I went to WP admin and checked out the taxonomy screen, and all of my categories returned! This worked and was easy to do, saving a bunch of time and a fun exercise to perform. Taking this effort to reuse existing content and not just huff and puff and recreate that same content will keep your database clean and your mind a little saner.

I hope you learned a little something about working on your site at a deeper level!

 
Last edited by a moderator:
Comment
Top