Custom Post type with custom taxonmy (category) and custom tag write rules
I need help to create a rewrite rule for my custom taxonomy (resource category) and custom tag (resource tag). My custom post type is this:
Resource – custom post type –
Resource type – Custom taxonomy
Resource Category – Custom category
Resource Tag – Custom Tag
Here is my code:
---Creating Custom post type---
$args = array(
'labels' => $labels,
'taxonomies' => array('resource_type'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
// array( 'slug' => 'offices','with_front' => false),
'capability_type' => 'page',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail','comments', 'editor' ),
'menu_icon' => 'dashicons-portfolio',
);
register_post_type( 'resources_post_type' , $args );
----
---Creating Custom Taxonmy---
$labels = array(
'name' => _x( 'Resource Types', 'Taxonomy General Name', 'fictionfirm' ),
'singular_name' => _x( 'Resource Type', 'Taxonomy Singular Name', 'fictionfirm' ),
'menu_name' => __( 'Resource Types', 'fictionfirm' ),
'all_items' => __( 'All Items', 'fictionfirm' ),
'parent_item' => __( 'Parent Item', 'fictionfirm' ),
'parent_item_colon' => __( 'Parent Item:', 'fictionfirm' ),
'new_item_name' => __( 'New Item Name', 'fictionfirm' ),
'add_new_item' => __( 'Add New Item', 'fictionfirm' ),
'edit_item' => __( 'Edit Item', 'fictionfirm' ),
'update_item' => __( 'Update Item', 'fictionfirm' ),
'view_item' => __( 'View Item', 'sfictionfirmnt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'fictionfirm' ),
'add_or_remove_items' => __( 'Add or remove items', 'fictionfirm' ),
'choose_from_most_used' => __( 'Choose from the most used', 'fictionfirm' ),
'popular_items' => __( 'Popular Items', 'fictionfirm' ),
'search_items' => __( 'Search Items', 'fictionfirm' ),
'not_found' => __( 'Not Found', 'fictionfirm' ),
'no_terms' => __( 'No items', 'fictionfirm' ),
'items_list' => __( 'Items list', 'fictionfirm' ),
'items_list_navigation' => __( 'Items list navigation', 'fictionfirm' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'insights')
);
register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );
-----
------Creating Custom Category---
$cat_labels = array(
'name' => _x( 'Resource Categories', 'Taxonomy General Name', 'fictionfirm' ),
'singular_name' => _x( 'Resource Category', 'Taxonomy Singular Name', 'fictionfirm' ),
'menu_name' => __( 'Resource Categories', 'fictionfirm' ),
'all_items' => __( 'All Items', 'fictionfirm' ),
'parent_item' => __( 'Parent Item', 'fictionfirm' ),
'parent_item_colon' => __( 'Parent Item:', 'fictionfirm' ),
'new_item_name' => __( 'New Item Name', 'fictionfirm' ),
'add_new_item' => __( 'Add New Item', 'fictionfirm' ),
'edit_item' => __( 'Edit Item', 'fictionfirm' ),
'update_item' => __( 'Update Item', 'fictionfirm' ),
'view_item' => __( 'View Item', 'sfictionfirmnt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'fictionfirm' ),
'add_or_remove_items' => __( 'Add or remove items', 'fictionfirm' ),
'choose_from_most_used' => __( 'Choose from the most used', 'fictionfirm' ),
'popular_items' => __( 'Popular Items', 'fictionfirm' ),
'search_items' => __( 'Search Items', 'fictionfirm' ),
'not_found' => __( 'Not Found', 'fictionfirm' ),
'no_terms' => __( 'No items', 'fictionfirm' ),
'items_list' => __( 'Items list', 'fictionfirm' ),
'items_list_navigation' => __( 'Items list navigation', 'fictionfirm' ),
);
$cat_args = array(
'labels' => $cat_labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'insights/%resource_type%/category')
);
register_taxonomy( 'resource_category', array( 'resources_post_type' ), $cat_args );
-----Creating Custom Tag---
$tag_labels = array(
'name' => _x( 'Resource Tags', 'Taxonomy General Name', 'fictionfirm' ),
'singular_name' => _x( 'Resource Tag', 'Taxonomy Singular Name', 'fictionfirm' ),
'menu_name' => __( 'Resource Tags', 'fictionfirm' ),
'all_items' => __( 'All Items', 'fictionfirm' ),
'parent_item' => __( 'Parent Item', 'fictionfirm' ),
'parent_item_colon' => __( 'Parent Item:', 'fictionfirm' ),
'new_item_name' => __( 'New Item Name', 'fictionfirm' ),
'add_new_item' => __( 'Add New Item', 'fictionfirm' ),
'edit_item' => __( 'Edit Item', 'fictionfirm' ),
'update_item' => __( 'Update Item', 'fictionfirm' ),
'view_item' => __( 'View Item', 'sfictionfirmnt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'fictionfirm' ),
'add_or_remove_items' => __( 'Add or remove items', 'fictionfirm' ),
'choose_from_most_used' => __( 'Choose from the most used', 'fictionfirm' ),
'popular_items' => __( 'Popular Items', 'fictionfirm' ),
'search_items' => __( 'Search Items', 'fictionfirm' ),
'not_found' => __( 'Not Found', 'fictionfirm' ),
'no_terms' => __( 'No items', 'fictionfirm' ),
'items_list' => __( 'Items list', 'fictionfirm' ),
'items_list_navigation' => __( 'Items list navigation', 'fictionfirm' ),
);
$tag_args = array(
'labels' => $tag_labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'insights/%resource_type%/tag')
);
register_taxonomy( 'resource_tag', array( 'resources_post_type' ), $tag_args );
Everything works just fine with custom post type url and resource type:
/insights/white-papers/my-first-white-paper-article (single article)
/insights/white-papers/ – white-papers – archive listing of all custom post types which have resource type => white-papers
For each resource type, there are one or more custom categories. When the user enters,
/insights/white-papers/category/football
or
/insights/case-studies/category/first-case
then it should view all custom post type for that particular resource type and for that custom category – archive listing.
Have searched for countless hours to find a rewrite rule to achieve what I want, but without any lucks.
I am gratefull if any of you help me out here:-)
Leave an answer