How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms

This answer is essentially the same as Ünsal’s, but I’d like to elaborate a bit because despite being right his answer didn’t help me (I was just trying to remember what I’d done in the past, I should have read his more carefully and I would have figured it out, but I think for others a full description would be useful).

To export the full category hierarchy you need to export “All Content”

Any other option won’t give you the full data (e.g. exporting a post with all categories tagged mostly works, but you lose all parentage/hierarchy data about taxonomies that are hierarchical).

When you export “All Content” the resulting file has a section with data about your terms. If you look through the file it should be easy enough to find, after authors and before “items” (i.e. posts).

You can just import this and delete content if that’s an option*

Importing the full file will replicate the taxonomy hierarchy perfectly, but also obviously import all the pages and posts too. If there aren’t too many than consider just deleting them by hand. It won’t take that long if you do it bulk and there are less than hundreds (remember you can use Screen Options on the Edit Posts screen to show more than the default and thus bulk edit more at a time).

If you have too much content to remove it all then you need to edit the WXR file by hand to remove everything but the terms you want to keep.

Editing the WXR file to remove content

This can be a bit tricky but really if you look at the contents of the file it’s not that hard to figure out what each element does. XML is like HTML with tags, so the main thing to be careful of is that you don’t delete a closing tag while leaving the opening tag etc.

You’ll find that there are 1-3 types of term definitions in the WXR, one type for Categories, one for “tags” and one for “terms”. “Custom taxonomies” use terms while old-fashioned categories and tags have their own special format.

CATEGORY EXAMPLE

<wp:category><wp:term_id>8880</wp:term_id><wp:category_nicename>runner-up-proposals</wp:category_nicename><wp:category_parent>second-round-proposals</wp:category_parent><wp:cat_name><![CDATA[Runner-up Proposals]]></wp:cat_name></wp:category>

TAG EXAMPLE

<wp:tag><wp:term_id>122</wp:term_id><wp:tag_slug>ave-maria</wp:tag_slug><wp:tag_name><![CDATA[Ave Maria]]></wp:tag_name></wp:tag>

CUSTOM TAXONOMY TERM EXAMPLE

<wp:term><wp:term_id>8579</wp:term_id><wp:term_taxonomy>gv_tools</wp:term_taxonomy><wp:term_slug>digital-video</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Digital Video]]></wp:term_name></wp:term>

So what you want to do is remove all content other than these category/tag/term tags from the WXR file. Namely wp:author tags that come before and the many item tags that come after. Most importantly: Don’t remove the closing /channel and /rss tags at the bottom! Without them the XML won’t validate.

OBVIOUSLY you will test the results of importing your edited file before using it on a live website. Try importing it into a fresh install locally and see if it fails completely, then check the categories listing screen in wp-admin and make sure everything is exactly as you expected.

Good luck!

Leave a Comment