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 CustomPress. 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:

1
2
3
4
5
6
7
8
9
10
11
function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

// Get all your post types
$post_types = get_post_types();

$query->set( 'post_type', $post_types );
return $query;
}
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

If you’d like to add only specific post types to listings of tags and categories you can replace the line:

1
$post_types = get_post_types();

with:

1
$post_types = array( 'post', 'your_custom_type' );

If you’d like to read more about the difference between custom post types, custom fields, and custom taxonomies you will be interested in this series of posts right here on WPMU.org.

Featured Plugin - WordPress Pop-Up Chat Plugin

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Featured Plugin - WordPress Facebook Plugin

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Featured Plugin - WordPress Ecommerce Shopping Cart Plugin

Out of all the WordPress ecommerce plugins available, MarketPress has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

Featured Plugin - WordPress Appointments Plugin

Take, set and manage appointments and client bookings without having to leave WordPress. Appointments+ makes it easy.
Find out more

Featured Plugin - WordPress Q&A Site Plugin

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

Featured Plugin - WordPress Infinite SEO Plugin

Fully integrated with the SEOMoz API, complete with automatic links, sitemaps and SEO optimization of your WordPress setup - this is the only plugin you need to help you rank your site number 1 on Google - nothing else compares.
Find out more

Featured Plugin - WordPress Membership Site Plugin

If you're thinking about starting a paid, or just private, membership site then this is truly the plugin you've been looking for. Easy to use, massively configurable and ready to go out of the box!
Find out more

Featured Plugin - WordPress Newsletter Plugin

Now there's no need to pay for a third party service to sign up, manage and send beautiful email newsletters to your subscriber base - this plugin has got the lot.
Find out more

Featured Plugin - WordPress Wiki Plugin

To get a wiki up and running you used to need to install Mediawiki and toil away for days configuring it... not any more! This plugin gives you *all* the functionality you want from a wiki, in WordPress!!!
Find out more

Comments (3)

  1. Craig, I’m really new to custom post types and while I make a little progress I keep ending back up where I started, which is looking for a good solution for posting press releases. I’d like to create a custom post type which includes the standard fields and also allows me to attach an image file and a PDF.

    I’ve tried using a lot of different plug-ins and tried writing my own code but nothing is working out for me. The ACF plug-in gets me close but I’m not sure how to call my single-press_release.php template. I’ve been working on this for over two days without any success.

    • Shaun,
      It can be kind of confusing if you’re just starting with CPTs, but I’d be happy to help if I can.
      Why don’t you post a link to your site, and I’ll see if I can work something up simultaneously on a test site and see if we can get it working together.

Participate