Snippets › Create Custom Tags for Custom Post Types (WordPress)

If you need to create custom tags for custom post types, here's what you need to do to add/allow "custom tags" to your custom post types in WordPress. These custom tags should only list your custom post type and not the other posts. Here are the steps.

1. Register taxonomy And Post Type

First you need to register taxonomy term for Post Tag. Edit your theme functions.php or plugin file to register taxonomy for custom tag like so.

//register taxonomy for custom post tags
register_taxonomy( 
'custom-tag', //taxonomy 
'my-custom-post', //post-type
array( 
    'hierarchical'  => false, 
    'label'         => __( 'My Custom Tags','taxonomy general name'), 
    'singular_name' => __( 'Tag', 'taxonomy general name' ), 
    'rewrite'       => true, 
    'query_var'     => true 
));

Then you need to register custom post type like this :

//register custom posts type
$snippet_pt_args = array(
     'labels' => array(
                    'name' => 'Custom Posts',
                    'singular_name' => 'Custom',
    ),
  'taxonomies' => array('custom-tag'),
  'public' => true,
  'menu_icon'   => 'dashicons-groups',
  'show_ui' => true,
  'rewrite' => array('slug'  => 'custom-post-slug', 
                    'with_front'     =>false, 
                    'menu_position'     => null),
                    'label'     => 'My Custom Posts'
                    );

register_post_type( 'my-custom-post', $snippet_pt_args);

So basically we can combine them and attach them to WordPress initialization hookinit like so:

add_action('init', '_question_register_post_type');
function _question_register_post_type(){
    //register taxonomy for custom post tags
    register_taxonomy( 
    'custom-tag', //taxonomy 
    'my-custom-post', //post-type
    array( 
        'hierarchical'  => false, 
        'label'         => __( 'Custom Tags','taxonomy general name'), 
        'singular_name' => __( 'Tag', 'taxonomy general name' ), 
        'rewrite'       => true, 
        'query_var'     => true 
    ));

    //register custom posts type
    $snippet_pt_args = array(
         'labels' => array(
                        'name' => 'Custom Posts',
                        'singular_name' => 'Custom',
        ),
      'taxonomies' => array('custom-tag'),
      'public' => true,
      'menu_icon'   => 'dashicons-groups',
      'show_ui' => true,
      'rewrite' => array('slug'  => 'custom-post-slug', 
                        'with_front'     =>false, 
                        'menu_position'     => null),
                        'label'     => 'My Custom Posts'
                        );

    register_post_type( 'my-custom-post', $snippet_pt_args);
}

Now go to WordPress admin dashboard and flush the rewrite rules by clicking "Save Changes" in "Permalink Settings". You should now be able to add custom tags to your Custom post types.

2. Retrieve Custom Post Types by their Tags

After the creation of custom post types with their own custom tags, you can now list them on a custom template.

Go to your theme directory and create a template called taxonomy-{custom-tag}.php. Replace "custom-tag" with your registered taxonomy term and also remove the brackets.

if(have_posts()){
    while (have_posts()){
        the_post();
        echo '<a href="'.get_permalink().'" title="'.$post->post_title.'" class="issue-text">'.$post->post_title.' </a>';
        the_content('');
    }
}

See your tag.php or index.php template file for more info.

3. Return Custom Tags associated with the post.

You can retrieve custom tags associated with the post like this :

$tag_list = $tags = get_the_term_list( $post->ID, 'custom-tag', '<ul class="snpt-tags"><li>','</li><li>','</li></ul>');
print $tag_list;

Hope it helps, suggestions are welcome. Thank you.