Jump to content
Sign in to follow this  
Alex

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

Recommended Posts

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().

1654351230_ScreenShot2019-06-27at5_49_54PM.thumb.png.cae7611ac8c4eea13a9bd922b584cdd7.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.

246180657_ScreenShot2019-06-27at5_53_09PM.png.032a0816c22b0aa0dc64bf3b884e89ed.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.

704353826_ScreenShot2019-06-27at5_56_26PM.png.6205f1343791ddd20a33ac43e72fac09.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.

1910435818_ScreenShot2019-06-27at6_07_04PM.png.0efc8fc8f6b9d79b66e74ac2c4d7af98.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!


- Alex
Check out the MD blog and follow me on Twitter.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...