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.
After this the CPT entry can be assigned Categories just like the standard Posts.
Reference: http://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type
Before:
After:
Great tips, I’ve got the way to fix my problem with this. Thanks!
Hi Sridhar – thanks for all the awesome tutorials. Amazingly useful.!
I was wondering how I can go about doing the same (adding category support) for the Portfolio Post Type Plugin that you suggest to use here. Right now I managed to make it display the post meta under the title, but the categories are empty even though I set them on the posts.
Thanks!
Dan.
Hi again, I actually figured it out – I used Bill Erickson’s tip from one of his comments: “My guess is you’re using a custom taxonomy like portfolio_category, not the built-in ‘category’ taxonomy that’s applied to posts. I used [post_terms taxonomy="portfolio_category"] to modify the post meta filter to reflect the custom taxonomy.
You might find it useful to mention that on the page 🙂
Cheers!
Dan.
Thanks for post, helped me alot 😀
you just saved me hours of work! thank you so much for posting your solution!
this code very help me, thanks
The code above did’t worked for me. I’ve changed the hook to the “wp_loaded” action and it worked. Thanks!
I have created a plugin for add custom post type (for example, news, reviews, etc.)
And I would like to show in the meta_info joined the date and author the name of the custom post type of each post.
https://www.evernote.com/l/APWCWXQGlL5PFbA8y0CHqy2brmxbCnhma3I
Hello Friend! I really thought the sensational post … But I’m a hard time .. I use The Events Calendar to schedule …. I managed to get the categories of event was to blog the loop … but when I search by category …. it does not load the postings …. How can I fix this?
Thank you very much!
[…] Reference: https://sridharkatakam.com/adding-categories-support-to-a-custom-post-type-in-wordpress/ […]
Oh my goodness. I have been trying to add standard categories and tags to a custom portfolio post type without success.
wp_loaded totally did the work for me.
Do you have a template for a custom post type category archive page in genesis? I also want to change the slug on the category archive page, how do I complete that?
thanks .thanks. thaaaaanks. i ‘d wasted my time for 1 week in the web and plugins to find solution. u r great. so what if i also want add main tags in CPT amin page?
Here you go.
add_action( 'init', 'sk_add_category_taxonomy_to_events' );
/**
* Link
e4gf_events
CPT to categories and tags taxonomies*/
function sk_add_category_taxonomy_to_events() {
register_taxonomy_for_object_type( 'category', 'e4gf_events' );
register_taxonomy_for_object_type( 'post_tag', 'e4gf_events' );
}
Thank you for posting this article. It’s really helpful!