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!