0

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 );
  • 1
    can you edit your question to add the code that registers your post type and taxonomy? – Milo Feb 12 '18 at 21:15
  • Whenever you try to change archives and/or permalinks for a CPT, you need to 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. – WebElaine Feb 12 '18 at 21:23
  • @Milo I edited my post with the code block. | @WebElaine thanks I'll research into it. Everything on this site seems so inner weaved I'm afraid updates like will break it haha but I'll look into the unregister_post_type() – Jose Feb 13 '18 at 15:42
1

A couple of things about your current code-

  1. You may find that pagination on your taxonomy term pages currently does not work. The order you register the taxonomy and post type matters here, as the rules are very similar and you need them to cascade a specific way. You can reverse the order they happen in by giving a later priority to the post type registration add_action( 'init', 'cpt_pt_resources', 1 );

  2. The faq_post_link function hooked to post_type_link at the end is redundant. The res_post_link function hooked to the same filter already does the same thing.

Enabling date archives is pretty straightforward. You need to add a set of rewrite rules, which are URL patterns in the form of regular expressions, plus the corresponding query vars the captured values get placed into.

function cpt_pt_resource_tax_date_archives() {

    add_rewrite_rule(
        'resources/([^/]+)/([0-9]{4})/?$',
        'index.php?resource-type=$matches[1]&year=$matches[2]',
        'top'
    );
    add_rewrite_rule(
        'resources/([^/]+)/([0-9]{4})/page/([0-9]{1,})/?$',
        'index.php?resource-type=$matches[1]&year=$matches[2]&paged=$matches[3]',
        'top'
    );

    add_rewrite_rule(
        'resources/([^/]+)/([0-9]{4})/([0-9]{2})/?$',
        'index.php?resource-type=$matches[1]&year=$matches[2]&monthnum=$matches[3]',
        'top'
    );
    add_rewrite_rule(
        'resources/([^/]+)/([0-9]{4})/([0-9]{2})/page/([0-9]{1,})/?$',
        'index.php?resource-type=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]',
        'top'
    );

}
add_action( 'init', 'cpt_pt_resource_tax_date_archives', 2 );

There are two sets of rules here- one for just year archives, plus pagination, and another for year/month archives, plus pagination.

Don't forget to flush rewrite rules after any changes. You can do this quickly by visiting the Settings > Permalinks admin page, which invokes the flush_rewrite_rules function.

If you want to add/change rules, or just get a better understanding of how they work, I recommend the Monkeyman Rewrite Analyzer plugin.

  • Thanks @Milo I'm about to give this a try in a bit in a staging environment. I'll report back. – Jose Feb 15 '18 at 14:53
  • So it worked, looks like I need to adjust the loop or the archive template to pull in the correct posts. @Milo – Jose Feb 15 '18 at 17:36
  • The rewrite rules should generate the correct query without needing to do anything further. I tested with twentysixteen theme and got the correct posts with just the default loop. – Milo Feb 16 '18 at 6:00
  • thanks again, it does seem to work. I tested it on the twentysixteen theme so we're troubleshooting the current theme where it is not working for some odd reason. – Jose Feb 20 '18 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.