Convert Custom Taxonomy Term to Custom Post Type

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.

Advertisements

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:

  1. Create and configure your target custom post type if it doesn’t already exist.
  2. 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)
  3. Save the code as a PHP file, e.g. tax-to-post.php
  4. 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 /wp-content/plugins/ directory
  5. 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.
Advertisements

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.

Advertisements

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:

  1. Delete the taxonomy and its terms.
  2. Create the new custom post type manually or using a plugin like CPTUI e.g Cars with URLs example.com/car/
  3. 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 example.com/car/suv/

If you don’t need to maintain the URL structure, then:

  1. Create the new custom post type manually or using a plugin like CPTUI e.g Vehicle
  2. Create new posts in this post type corresponding to the terms you want to convert e.g Saloon, SUV, Pickups etc
  3. Advertisements
  4. 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 example.com/car/suv to example.com/vehicle/suv
  5. Delete the taxonomy if you no longer need it.

Note: Test both of these on a staging site before trying on a live site.

Tags:

Author

Kelvin Muriuki is a web content developer that's passionate about keeping the internet a useful place. He is the founder and editor of Journey Bytes, a tech blog and web design agency. Feel free to connect with him regarding the content appearing on this page or on web and content development.

Leave a Reply

Feel free to share your comments or questions with me. I may not be able to respond immediately so please check later once I've approved your comment.

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