Tag list & tag index for custom post type

WordPress issue:

I have a custom post type: custom-post-type.

register_post_type( 'custom-post-type', array(
  'label' => __('Custom Post Type'),
  'public' => true,
  'show_ui' => true,
  '_builtin' => false, // It's a custom post type, not built in
  '_edit_link' => 'post.php?post=%d',
  'capability_type' => 'post',
  'hierarchical' => false,
  'rewrite' => array("slug" => "custom-posts"), // Permalinks
  'query_var' => "custom-post-type",
  'menu_position' => 30,
  'supports' => array('title', 'excerpt', 'editor', 'thumbnail',),
  'taxonomies' => array('category', 'custom-taxonomy'),
  'has_archives' => true,
));

I have a taxonomy: custom-taxonomy

register_taxonomy(
  'custom-taxonomy',
  array('custom-taxonomy'),
  array(
    'hierarchical' => false,
    'labels' => array (
    'name' => _x( 'Custom Taxonomy', 'taxonomy general name' ),
      'singular_name' => _x( 'Custom Taxonomy', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Custom Taxonomy' ),
      'all_items' => __( 'All Custom Taxonomy' ),
      'edit_item' => __( 'Edit Custom Taxonomy' ), 
      'update_item' => __( 'Update Custom Taxonomy' ),
      'add_new_item' => __( 'Add New Custom Taxonomy' ),
      'new_item_name' => __( 'New Custom Taxonomy' ),
      'menu_name' => __( 'Custom Taxonomy' ),
    ),
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'custom-taxonomy', 'with_front' => true),
  )
);

I need to be able to create a list of tags based on the tags assigned to custom-post-type. I would need each tag to link to an index page for that tag, specific to the custom post type.

For example:

\tag\mytag brings back each post with the tag mytag.
\custom-posts\tag\mytag would bring back each posts of type custom-posts with the tag mytag.

I’m hoping to turn any solution in to a WordPress plugin.

=============================================================
EDIT: Big breakthrough.

WordPress rewrites are a bit of a black-box mystery to me. I was suspicious that rewrite rules may be playing a part in my struggles, but I couldn’t get a handle on debugging the rules.

In steps The Monkeyman Rewrite Analyzer. This plugin details your WordPress rewrite rules and lists them all in order of application. Furthermore, you may pass in a path to find out which rules match and in what order.

After some tweaks and further investigation, I had changed the rewrite path for the taxonomy to “custom-posts/tag”. This put in place the correct rewrite rule, but the rewrite analyzer suggested that /custom-post/tag/mytag was matching against post attachments, but the taxonomy rewrite was present further down the list.

I tried swapping the order in which I declared my taxonomy and post type, and I changed some of the details such as the taxonomy rewrite rule. With the addition of a dummy taxonomy-custom-taxonomoy.php I have a page that does not 404.

register_taxonomy(
  'custom-taxonomy',
  array('custom-post-type'),
  array(
    'hierarchical' => false,
    'labels' => array (
      'name' => _x( 'Custom Taxonomy Tags', 'taxonomy general name' ),
      'singular_name' => _x( 'Custom Taxonomy Tag', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Custom Taxonomy Tags' ),
      'all_items' => __( 'All Custom Taxonomy Tags' ),
      'edit_item' => __( 'Edit Custom Taxonomy Tags' ), 
      'update_item' => __( 'Update Custom Taxonomy Tags' ),
      'add_new_item' => __( 'Add New Custom Taxonomy Tags' ),
      'new_item_name' => __( 'New Custom Taxonomy Tag' ),
      'menu_name' => __( 'Custom Taxonomy Tags' ),
    ),
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'custom-posts/tag', 'with_front' => true),
  )
);
register_post_type( 'custom-post-type', array(
    'label' => __('custom Post'),
    'public' => true,
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in
    '_edit_link' => 'post.php?post=%d',
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => array("slug" => "custom-posts"), // Permalinks
    'query_var' => "custom-posts",
    'menu_position' => 30,
    'supports' => array('title', 'excerpt', 'editor', 'thumbnail',),
    'taxonomies' => array('category', 'custom-taxonomy'),
    'has_archives' => true,
));

Next I will figure how to make the template display the articles, but I am back in familiar territory!

Maybe this won’t need a plugin after all!

1 Answer
1

There is a WordPress function called get_terms. This will provide you with the output of your custom taxonomy throughout your website.

I’ve also included insight as to how to use WordPress’s native tag support when you register your custom post type and custom templates for taxonomies.

If you wish to retrieve all instances of your custom taxonomy and format a list, you can use get_the_term_list and use your custom taxonomy as parameter 2 of the function.

<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?> 

If you wish to retrieve the instances of your custom taxonomy on a per post basis, use wp_get_post_terms and use your custom taxonomy as parameter 2 of the function.

<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ) ?>

When creating a custom post type, you can set the argument ‘taxonomies’ to utilize WordPress’s native post tag functionality.

'taxonomies' => array('category', 'tags', 'custom-taxonomy'),

This would then enable you to utilize the functions offered for tags such as the_tags or wp_tag_cloud

You should also consider a taxonomy.php because WordPress defaults the display of any taxonomy from taxonomy.php. This would also allow you customize the appearance of specific taxonomies due to having files specific to that term. taxonomy-{taxonomy}-{slug}.php. Visit Template Hierarchy

Leave a Comment