Adding post tags to custom post types

The question:

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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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),
    )
);

Method 2

Easy. Funny enough I was tackling that very same issue myself.

Get yourself the “Custom Post Type UI” plugin. Create a custom taxonomy for your custom post type, called “tag”.

Whenever the user creates a new custom post, she will always be able to add a tag.

This tag should then be available in the form of a permalink and deliver all the custom post that are labeled under it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment