Add Custom Post Types to Tags and Categories in WordPress
WordPress gives you the ability to add custom post types and taxonomies easily with some simple code in your functions.php file, or through the use of a plugin like Custom Post Type UI. That means you can quickly turn your WordPress site into a fairly well-featured CMS.
The only problem is, out of the box, WordPress does not automatically take your newly created custom post types and add them to the archive pages for existing taxonomies ‘categories’ or ‘tags.’
That means if you created a custom post type called “movies” and used the native WordPress taxonomy “category” to categorize and order that custom post type, simply navigating to the archive for that category will not show your custom post type in the list of posts for that archive.
For example, you created a movie review site and used standard WordPress categories and tags with that custom post type so you could relate those reviews to your other posts or pages. Let’s say you decided to use a category of “drama.” Navigating to the category archive for drama, namely http://yoursite.com/category/drama will not show the custom post type you just added – even though you selected the drama category.
This is frustrating for those who go through the trouble to add plan out a custom post type, taxonomies, add the content and then find out it doesn’t display correctly.
Some would call this a bug, but the developers at WordPress maintain that sorting archives is for normal post types for which WordPress was created, and adding in that functionality for custom post types requires some custom code. And indeed it does.
So, if you’d like your custom post types to show up in archive listings for your site’s standard tags and categories you can add the following code to your functions.php file:
If you’d like to add only specific post types to listings of tags and categories you can replace the line:
If you’d like to read more about the difference between custom post types, custom fields, and custom taxonomies, check out this post from our blog.Tags: