Quality Work Make Your Dream Come True
WordPress BuddyPress bbPress Developer

How To Add Categories Support for a Custom Post Type in Your WordPress

A great user requested me a feature of wordpress tooltips plugins: add tooltips categories for tooltips, so he will more easier to manage tooltips in back end, I think this is a great feature request and I have realized this.

In general, as a developer, when you search add category support for custom post type(our tooltip is based on custom post type:tooltips), you will find the code like this:

$labels = array(
‘name’ => __(‘Tooltips’, ‘tooltips’),
‘singular_name’ => __(‘Tooltip’, ‘tooltips’),
‘add_new’ => __(‘Add New’, ‘tooltips’),
‘add_new_item’ => __(‘Add New Tooltip’, ‘tooltips’),
‘edit_item’ => __(‘Edit Tooltip’, ‘tooltips’),
‘new_item’ => __(‘New Tooltip’, ‘tooltips’),
‘all_items’ => __(‘All Tooltips’, ‘tooltips’),
‘view_item’ => __(‘View Tooltip’, ‘tooltips’),
‘search_items’ => __(‘Search Tooltip’, ‘tooltips’),
‘not_found’ => __(‘No Tooltip found’, ‘tooltips’),
‘not_found_in_trash’ => __(‘No Tooltip found in Trash’, ‘tooltips’),
‘menu_name’ => __(‘Tooltips’, ‘tooltips’)
);

$args = array(
‘labels’ => $labels,
‘public’ => false,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘_builtin’ => false,
‘query_var’ => “tooltips”,
‘rewrite’ => false,
‘taxonomies’ => array( ‘category’ ),
‘capability_type’ => ‘post’,
‘has_archive’ => false,
‘hierarchical’ => false,
‘menu_position’ => null,
‘supports’ => array( ‘title’, ‘editor’,’author’,’custom-fields’,’thumbnail’ )
);
register_post_type(‘tooltips’, $args);

And yes in back end, you will find the category in menu item of your custom post type, like this:
tooltips-category

There are another method to do this in your plugin or your theme:

add_action( ‘init’, ‘support_category_taxonomy_for_tooltips’ );
function support_category_taxonomy_for_tooltips() {
register_taxonomy_for_object_type( ‘category’, ‘tooltips’ );
}

You can add this code to your plugin or functions.php in your theme folder, it seems simple? Wait. 🙂

If you use this code, yes, you can add category for your wordpress custom post type, but the problem is the category is “shared” with wordpress default post type, it means in your category, maybe have posts from wordpress standard post and maybe have posts from your custom post type, it is still not easy to be managed in back end, right? 🙂

The solution is use register_taxonomy to register a taxonomy for your custom post type and setting the name as ‘Categories’, so it looks like this:

$catlabels = array(
‘name’ => ‘Categories’,
‘singular_name’ => ‘Tooltips Categories’,
‘all_items’ => ‘All Tooltips’,
‘parent_item’ => ‘Parent Tooltips’,
‘edit_item’ => ‘Edit Tooltips’,
‘update_item’ => ‘Update Tooltips’,
‘add_new_item’ => ‘Add New Tooltips’,
‘new_item_name’ => ‘New Tooltips’,
);

$args = array(
‘label’ => ‘Categories’,
‘labels’ => $catlabels,
‘public’ => true,
‘hierarchical’ => true,
‘show_ui’ => true,
‘show_in_nav_menus’ => true,
‘args’ => array( ‘orderby’ => ‘term_order’ ),
‘rewrite’ => array( ‘slug’ => ‘tooltips_categories’, ‘with_front’ => false ),
‘query_var’ => true
);

register_taxonomy( ‘tooltips_categories’, ‘tooltips’, $args );

By this way, you will have functionality of wordpress standard category, also you can own a unique category for your custom post type, so for your users, it is very easy to manage your custom post type in back end, looks easy? Sure, this is codes in my new version wordpress tooltips 3.7.9, it have more features and be testing and will be released a few days! 🙂

Dear readers, thanks for read my blog, I will always share the best wordpress how to tips with you, thanks, have a great day with your family. 🙂

Best Regards,
Tomas

Leave a Reply