Adding Categories support to a Custom Post Type in WordPress

Posted on

When a CPT is manually created in WordPress using register_post_type function it can be linked to the built-in category taxonomy via

'taxonomies'          => array( 'category' ),

When register_post_type() is defined without support for categories and is coming from a plugin that you can (should) not modify, it is still possible to add categories support to the CPT by adding the following in child theme’s functions.php:

//* Link e4gf_events CPT to categories taxonomy
add_action( 'init', 'sk_add_category_taxonomy_to_events' );
function sk_add_category_taxonomy_to_events() {
	register_taxonomy_for_object_type( 'category', 'e4gf_events' );
}

where e4gf_events is the Post Type Key. This can be easily seen by going to CPT listing page in the backend and looking at the URL.