How to Add Categories to a Custom Post Type in WordPress

Harnessing the Power of Custom Post Types and Taxonomies in WordPress

WordPress makes it easy for anyone to create custom post types and taxonomies. According to BuiltWith, over 4 million websites have added custom post types. With some planning and basic code, you too can tap into these powerful content modeling tools.

In this beginner‘s guide, we‘ll explore:

  • Key benefits of custom post types and taxonomies
  • How to add category support to custom post types
  • Displaying multiple post types in category archives
  • Best practices for using custom post types and taxonomies

Let‘s dive in!

Why Use Custom Post Types and Taxonomies?

Out of the box, WordPress comes with posts and pages. But for many sites, more content types are needed:

  • Product catalog
  • Staff directory
  • Docs and downloads
  • Events calendar
  • Portfolio projects
  • Testimonials

Posts get categorized using tags and categories. But custom taxonomies power more precise data structure:

  • Genres
  • Brands
  • Skills
  • Locations

According to WP Engine, over 50% of their hosted sites use custom post types. Taxonomies provide unlimited flexibility.

With custom post types and taxonomies you can:

  • Go beyond just blog posts and basic pages
  • Craft custom content models tailored to your site
  • Flexible display options (custom archives, theme templates, etc)
  • API integrations, forms, and mapping made easy

Let‘s look at how to register these and work with categories.

Adding Category Support to Custom Post Types

By default, WordPress registers the "post" and "page" types. Posts support the built-in category and tag taxonomies.

Custom post types are independent with no taxonomies enabled unless specified. Here is how to add category support:

Registering the Post Type and Taxonomies

Use the register_post_type() and register_taxonomy() functions.

// Register Custom Post Type
function create_portfolio_cpt() {

  $labels = array(
    ‘name‘ => ‘Portfolio‘,
    ‘singular_name‘ => ‘Portfolio Item‘,
  );

  $args = array(
    ‘labels‘ => $labels,
    ‘public‘ => true,
    ‘has_archive‘ => true,
    ‘taxonomies‘ => array( ‘category‘ ), // Enable categories
  );

  register_post_type( ‘portfolio‘, $args );

}
add_action( ‘init‘, ‘create_portfolio_cpt‘ );

Now the "portfolio" custom post type supports categories. Add multiple taxonomies like:

‘taxonomies‘ => array( ‘category‘, ‘post_tag‘ )

55% of sites using custom post types leverage categories according to Custom Post Type UI stats.

Display in Category Archives

Category archives will only include core "post" posts by default.

To add custom post types, filter the main query:

function add_custom_types( $query ) {
  if( is_category() ) {
    $post_types = array(‘post‘, ‘portfolio‘, ‘event‘);
    $query->set( ‘post_type‘, $post_types );
  }
}
add_action( ‘pre_get_posts‘, ‘add_custom_types‘ );

Now the category will include portfolio and event posts when assigned to the same categories as regular posts.

Powerful Custom Taxonomy Options

Categories provide an easy starting point. But custom taxonomies open up flexibility:

// Register Custom Taxonomy
function create_location_taxonomy() {

  $labels = array(
    ‘name‘ => ‘Locations‘
  );

  register_taxonomy( ‘location‘, array(‘portfolio‘), array(
      ‘labels‘ => $labels
  ));

} 
add_action( ‘init‘, ‘create_location_taxonomy‘);

Now you can create location terms like "New York", "Paris", and assign them to portfolio posts!

Integrations and Data Mapping

Taxonomies provide the structure for powerful integrations.

For example, map portfolio categories to schema.org types for rich SEO:

function rich_snippets( $data, $post ) {

  if ( $post->post_type == ‘portfolio‘ ) {

    $type = wp_get_post_terms($post->ID, ‘category‘);

    $data[‘type‘] = $type[0]->slug;

  }

  return $data;

}
add_filter( ‘wpseo_schema_graph_pieces‘, ‘rich_snippets‘ ); 

Many plugins extend custom post functionality including Advanced Custom Fields, Toolset, and More Fields.

Conclusion

With some planning, custom post types and taxonomies help you take modeling WordPress content to the next level.

Start simple with the building blocks like posts, pages, and categories. Then extend with custom models tailored exactly to your site and integrations.

The possibilities are truly endless when harnessing these powerful WordPress features. Let us know if you have any other questions!

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.