WordPress: How to Change the Custom Post Type for All Posts

Programmers

How to Change the Custom Post Type for All PostsIf you need to change the Custom Post Type of hundreds of posts it might seem a bit daunting. Where does WordPress hold that information? In the database? Is there a way I can move it without spending hours of meaningless time?

Yes. In fact, you can change the custom post type incredibly easily using the WordPress Export function.

When To Use

There are several situations you could find yourself in where this would be helpful, but the most likely one is when the theme has a built-in custom post type. When you go to change the theme, that custom post type is no longer there. Whether you have to switch it to a new theme’s custom post type or use a plugin’s custom post type, either way, you’ll need to export and import that information.

Note: Ideally, of course, custom post types should never be included in a theme – they should always be in a plugin. But since we don’t live in an ideal world, you’ll probably need this information at some point!

Basic Problem

Wordpress Change Custom Post TypeWhile Custom Post Types are incredibly useful, the problem you face at this point is that they each have a unique identifier. Let’s say you have a Custom Post Type with a unique ID “videos”. The new Custom Post Type may be named “kjf_videos”, which would not recognize the posts that are “videos”.

While the Admin panel link in the sidebar may show both Custom Post Types page titles as “Videos”, if the unique ID is different, WordPress will view them as completely different Custom Post Types.

The Solution

The solution is surprisingly simple.

Step 1 – Export the old Custom Post Type

We’ll need to get the information into an easily editable file. Go to TOOLS -> EXPORT and choose the Custom Post Type you want to edit. Click Download Export File. You’ll get an “.xml” file.

Step 2 – Find the old Custom Post Type ID and the new Custom Post Type ID.

To find the old and the new, you’ll need to link at URLs. Go to the old Custom Post Type Page (for example, click on Videos in the Admin panel). Take a look at the URL:

Custom Post Type Unique ID

The section that says “post_type=video” is the part you want. In this example, the post type unique ID is “video”. Not really unique in this case, but that’s what we’ll need.

You’ll want to do the same thing with the new Custom Post Type. Go to the page in the backend, and look at the URL. Write down both exactly.

Step 3 – Editing the Information

Now we’ll go back to our “xml” file we downloaded. You’ll want to open this file in a program that allows you to find and replace in bulk. Trust me. You do not want to do this by hand. I used Coda by Panic, Inc.

xmlfileThe file will look like the image on the right; a little confusing, but don’t worry, it’s fairly simple to use.

In the SEARCH area, you want to place the following text:

<wp:post_type>

Find the first option, and notice what it says directly after. If the old unique ID is “videos” the whole line should look like this:

<wp:post_type>videos</wp:post_type>

All we’re doing is finding and replacing that line with our new unique ID.

Now we’re ready. Go to the SEARCH bar and add the post type line with the old unique ID:

<wp:post_type>videos</wp:post_type>

and for the REPLACE box, add in the new unique ID in place of the “videos” section:

<wp:post_type>new_unique_id</wp:post_type>

And click to change all the instances. 

Next, you’ll need to change one more area. SEARCH for the following, but replace “video” with your old unique ID:

?post_type=video

And in the REPLACE box, add in the following with your new unique ID:

?post_type=new_unique_id

And click to change all the instances.

Step 4 – Import the information back into WordPress

The final step is simply importing that file back into WordPress. Go to TOOLS -> IMPORT and choose that file to import. If all the search and replaces went correctly, the information will show up in the new post type!

Congrats!

A little Extra – Changing Categories and Tags

You might, at times, need to also edit the categories or tags (also known as taxonomies) for the Custom Post Type. These also have unique IDs, just like the Custom Post Type, so you can edit them directly in the xml file.

The section that you’ll need to search and replace looks like this:

<category domain="video_category"
<category domain="video_tag"

The video_category is the category unique ID. Tags are done in the same area, as you can see in the “video_tag” above.

Find the old and new Unique IDs for your Taxonomies (categories, tags, etc.)

Go to the VIDEOS -> CATEGORIES (or your Custom Post Type -> Categories/Tags/Taxonomy) and in the URL you’ll see something like this:

?taxonomy=categories&post_type=video

The “taxonomy=categories” will tell you the unique ID of the taxonomy you’re editing. Do this to find both the old and the new unique IDs.

Search and Replace

Next, do a search and replace in your XML file, searching for the old and replacing it with the new unique ID:

<category domain="video_category"
<category domain="video_tag"

That’s it! You’ll be ready to import the file into WordPress with all the new taxonomies!

Comments