Building websites is almost like constructing buildings. By that I mean, a well laid out plan must factor for future possibilities such as redesigns, expansions, repairs and demolitions.
In relation to websites, the same fact holds true. A well laid out web design that factors in future growth may save you a lot of work when refactoring and cleaning things up.
This is the lesson I learnt recently with one of my WordPress sites, when I realized that a taxonomy I was using had more potential beyond just categorizing the existing content.
As the site had grown, the terms proved that they would be far more valuable as standalone sources of information that could be updated rather than simple categories.
So I knew one way or another I had to convert them to posts since taxonomies, even with content custom fields, would still be tied to the existing post type.
Fortunately, I found a quick online solution to convert my terms into posts in bulk, which I’ve wanted to share for some months now. I’ve also included a manual method if you just have a few terms to convert.
Bulk Convert Terms to Custom Post Type Posts
Some inspired searching landed on this GitHub gist by Strap1 which I eventually used to convert my 42 terms to posts in my existing custom post type. The code also converts any existing taxonomy description into the post content.
Here’s how to use it:
- Create and configure your target custom post type if it doesn’t already exist.
- Copy the code to an editor and edit the following portions to correspond to the taxonomy you’ll be converting and the target post type.
$taxonomy = 'your_taxonomy'; //Define Custom Taxonomy (source)
$post_type = 'your_CPT'; // Define Custom Post Type (target)
- Save the code as a PHP file, e.g. tax-to-post.php
- Next, we need to install this file as a custom plugin. To do that, just copy PHP file in a folder (e.g tax-to-post) inside the
- Go to your WordPress site and confirm the plugin has been installed. Activate it to begin the conversion. I suggest trying it first on a staging environment before running it on a live site.
A Note on URLs
The URL structure of the converted posts will depend on how you’ve configured the custom post type. To avoid issues, make sure the term URLs are NOT similar to the custom post type URLs. You can thereafter redirect the term’s URL to their corresponding posts.
You can however also maintain the URL structure after conversion. To do that, first make sure that your custom post type URL structure is configured accordingly.
Next, edit the post status portion in the code to save the posts as drafts after conversion instead of publishing them, i.e
'post_status' => 'draft'.
Now, when you run the plugin, the converted posts should be saved as drafts. After doing this, delete the taxonomy from your site to “release” the URLs then publish the posts in their original term URLs.
This is what I successfully did on my site, however you may want to test this first on a staging site to make sure it works as intended.
Convert Taxonomy Terms to Post Types Manually
This is the long hard way assuming you have many terms. On the other hand, if you have only a handful of terms this is very feasible.
We can have two possible scenarios here: either you need or don’t neeed to maintain the URL structure.
To maintain the URL structure, in theory all you have to do is:
- Delete the taxonomy and its terms.
- Create the new custom post type manually or using a plugin like CPTUI e.g Cars with URLs
- Create new posts in these post type corresponding to the terms you want to convert e.g. Saloon, SUV, Pickups etc. The post’s URLs structure should be exactly similar to the terms URL i.e. if a term URL was
example.com/car/suv/its post slug will be just
suv, making the full URL
If you don’t need to maintain the URL structure, then:
- Create the new custom post type manually or using a plugin like CPTUI e.g Vehicle
- Create new posts in this post type corresponding to the terms you want to convert e.g Saloon, SUV, Pickups etc
- If the URLs are indexed on search engines, you may permanently redirect the terms’ URLs to their corresponding posts e.g set a 301 redirect of the term
- Delete the taxonomy if you no longer need it.
Note: Test both of these on a staging site before trying on a live site.