Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Add Already Registered Taxonomy to Custom Post Type

Posted on July 9, 2011 by Pippin in Custom Post Types, Intermediate, Quick Tips, Taxonomies 9 Comments
Home» Tutorials » Custom Post Types » Add Already Registered Taxonomy to Custom Post Type
Tweet
Love It - 2

Sometimes, when using a custom post type in your plugins (or themes), it is desirable to add the already-registered taxonomies (Post Tags and Categories by default) to your custom object type. This quick code snippet will show you how to do that.


This is a quick tip. Check out more Quick Tips

Using the register_taxonomy_for_object_type(), we will specify a taxonomy and an object type, then hook our function into an init hook.

1
2
3
4
add_action('init','add_categories_to_cpt');
function add_categories_to_cpt(){
    register_taxonomy_for_object_type('category', 'post_type_name');
}

This will work fine, but sometimes the init hook won’t always work. If you your custom post type is created in a plugin, you may need to use the plugins_loaded hook instead, which fires after all plugins have been loaded. Like this:

1
2
3
4
add_action('plugins_loaded','add_categories_to_cpt');
function add_categories_to_cpt(){
    register_taxonomy_for_object_type('category', 'post_type_name');
}

Enjoy!

Tweet Follow @pippinsplugins
custom post type, taxonomy

9 comments on “Add Already Registered Taxonomy to Custom Post Type”

  1. Jeroen says:
    July 11, 2011 at 7:53 am

    I want to have Post Tags added to the Custom Post Types created. Which file do I need to add the code to? Is there another way to have the Post Tages enabled?

    Reply
    • pippin says:
      July 11, 2011 at 12:57 pm

      Just use this: register_taxonomy_for_object_type('post_tag', 'post_type_name');

      Put it in your functions.php, or plugin file if you’re writing a plugin.

  2. Jeroen says:
    July 11, 2011 at 2:15 pm

    Hmm.. don’t get it … I’m using the Easy Content Types plugin and want to have the post tags available in the created Content Types. Which functions.php do you mean, from the theme, wordpress or …

    Reply
  3. pippin says:
    July 11, 2011 at 3:22 pm

    @Jeroen – Ah, you should have said so ;) There’s a section in the documentation that says how to do it. Go to the help page and look in the taxonomy section.

    In the future when asking about a plugin, please comment on the plugin post.

    Reply
  4. Jeroen says:
    July 12, 2011 at 8:11 am

    Should have done that first.. missed the part in the docs. Got it working now! Thanks!

    Reply
  5. sstasio says:
    October 3, 2012 at 2:42 pm

    Pippin,

    I’ve been trying to add the categories to your sugar calendar plugin using this code, but nothing seems to happen. I’ve tried it both ways. Here is the code I’ve tried:

    ‘
    //Register categories with events
    add_action(‘init’,'add_categories_to_cpt’);
    function add_categories_to_cpt(){
    register_taxonomy_for_object_type(‘category’, ‘sc_event’);
    }
    ‘
    and
    ‘
    //Register categories with events
    add_action(‘plugins_loaded’,'add_categories_to_cpt’);
    function add_categories_to_cpt(){
    register_taxonomy_for_object_type(‘category’, ‘sc_event’);
    }
    ‘
    and yes, I put these in the funtions file.

    Sean in SF

    Reply
    • Pippin says:
      October 4, 2012 at 2:17 pm

      Change

      add_action(‘init’,'add_categories_to_cpt’);

      to

      add_action(‘init’,'add_categories_to_cpt’, 200);
  6. Aldi says:
    March 10, 2013 at 1:25 am

    Thanks for the tips! But how can I tell if I had successfully add a custom taxonomy to a custom post type with this method? Is there any way to check the taxonomy’s relationship with the post type?

    Reply
    • Pippin says:
      March 10, 2013 at 8:40 pm

      That’s a completely different topic than this post.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Custom Taxonomy Friendly Archives
  • Password Protect Taxonomy Terms and All Posts in the Terms
  • Adding Custom Meta Fields to Taxonomies
  • Filter Posts by Custom Taxonomy in Admin
  • Post Types Column Editor for WordPress

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

attachments contextual help Sugar Event Calendar wp_enqueue_script hook campaign monitor Tom McFarlin shortcodes featured Rémi Corson the_content register_setting add_options_page login forms short codes Related posts plugin authors do_action attachment mail chimp image comments recent posts apply_filters post types bbpress short code taxonomies custom post type gallery Ajax images Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins