Custom post type yearly/ monthly archive permalinks

Here’s an example using add_rewrite_rule to handle years and months for a custom post type where news is the slug. Visit the Settings > Permalinks page in admin to flush rewrite rules after this is added. You could also put this in a plugin and flush rewrite rules on plugin activation.

function wpa83797_news_rewrite_rules(){

    add_rewrite_rule(
        'news/([0-9]{4})/([0-9]{1,2})/?$',
        'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]',
        'top'
    );

    add_rewrite_rule(
        'news/([0-9]{4})/?$',
        'index.php?post_type=news&year=$matches[1]',
        'top'
    );

}
add_action( 'init', 'wpa83797_news_rewrite_rules' );

Leave a Comment