Adding post tags to custom post types

Standard WordPress post types have “Post Tags” which can be used to “tag” posts with similar content, makes those similar posts easily accessible just by opening that particular tag page and receiving the list of all posts.

I need exactly the same thing with my custom post types:

  • ability to tag the post with multiple tags
  • ability to list all custom posts tagged with the same tag by opening that tag page

How do I add support for adding tags to my custom post types?

2 Answers
2

I actually managed to figure it out and did it just beside the code of my custom post type. Like Fred suggested, I need to add a taxonomy to my custom post type called “tag”.

And this is the way to do it:

register_taxonomy
(
    'myposttype-tag',
    array('myposttype'),
    array
    (
        'hierarchical' => false,
        'labels' => array
        (
            'name' => _x( 'My Post Type Tags', 'taxonomy general name' ),
            'singular_name' => _x( 'My Post Type Tag', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search My Post Type Tags' ),
            'all_items' => __( 'All My Post Type Tags' ),
            'edit_item' => __( 'Edit My Post Type Tag' ), 
            'update_item' => __( 'Update My Post Type Tag' ),
            'add_new_item' => __( 'Add New My Post Type Tag' ),
            'new_item_name' => __( 'New My Post Type Tag Name' ),
            'menu_name' => __( 'My Post Type Tags' ),
        ),
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'myposttype-tag', 'with_front' => true),
    )
);

Leave a Comment