Hello WP StackExchange,
I picked up and it deals with Custom Post Type archives.
So we have a CPT called Resources. Under Resources we have a custom taxonomy called Resource Types. For Resource Types we have Media, Gallery, Blog, FAQs.
So each with their own permalink:
/resources/media/
/resources/blog/
etc...
You're probably wondering why have the blog posts under Resources, good question, based on the last developers notes and what marketing team was going for is they wanted the blog to have that permalink structure. Using the regular Posts type there was some conflict issues when the base was added in the Permalink settings for the Posts. 2-3 other developers have touched this and I'm hoping I can finalize it. This route isn't the best I know but it's what I'm working with :/ any help is appreciated.
What I'm trying to fix is the archives for the blog. So if we hit /resources/blog/2012/02/ we get a 404. I been looking and can't find a way to get this to work or if I can be pointed to the right direction. I tried some archive templates but nothing seemed to work for me.
Also some quick backstory, we have duplicate posts under Posts and Resources. So if you hit /2012/02 you get the feed from Posts. We are going to delete the posts under Posts so that Resources would be the only method for the blog.
EDIT: Code for CPT and Taxonomy
// Resource
function cpt_pt_resources() {
$labels = array(
'name' => _x( 'Resources', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Resource', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Resources', 'text_domain' ),
'name_admin_bar' => __( 'Resources', 'text_domain' ),
'archives' => __( 'Resource Archives', 'text_domain' ),
'attributes' => __( 'Resource Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'add_new_item' => __( 'Add New Resource', 'text_domain' ),
'add_new' => __( 'Add Resource', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'view_items' => __( 'View Items', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Resource', 'text_domain' ),
'description' => __( 'Resources', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'page-attributes', 'post-formats', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag', ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-index-card',
'show_in_admin_bar' => false,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array( 'slug' => 'resources/%resource-type%', 'with_front' => false ),
);
register_post_type( 'resource', $args );
}
add_action( 'init', 'cpt_pt_resources', 0 );
// Resource URL rewrite
function res_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'resource-type' );
if( $terms ){
return str_replace( '%resource-type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'res_post_link', 1, 3 );
// Custom taxonomy Resource Type
function cpt_pt_resource_tax() {
$labels = array(
'name' => _x( 'Resource Types', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Resource Type', 'taxonomy singular name', 'textdomain' ),
'menu_name' => __( 'Resource Types', 'textdomain' ),
'search_items' => __( 'Search Resource Types', 'textdomain' ),
'all_items' => __( 'All Resource Types', 'textdomain' ),
'parent_item' => __( 'Parent Resource Type', 'textdomain' ),
'parent_item_colon' => __( 'Parent Resource Type:', 'textdomain' ),
'edit_item' => __( 'Edit Resource Type', 'textdomain' ),
'update_item' => __( 'Update Resource Type', 'textdomain' ),
'add_new_item' => __( 'Add New Resource Type', 'textdomain' ),
'new_item_name' => __( 'New Resource Type Name', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'menu_position' => 0,
'rewrite' => array( 'slug' => 'resources', 'with_front' => false ),
);
register_taxonomy( 'resource-type', array( 'resource' ), $args );
}
add_action( 'init', 'cpt_pt_resource_tax', 0 );
// Custom URL for FAQs
function faq_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_tax( 'resource-type', 'faq') ){
$terms = wp_get_object_terms( $post->ID, 'faqs' );
if( $terms ){
return str_replace( '%resource-type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'faq_post_link', 1, 3 );
unregister_post_type()
and then re-register with the new settings. Unregistering won't delete any of the existing posts, it just clears the permalink settings so when you re-register you can override whatever structure you had in the past.unregister_post_type()