Jacob’s comment got me on the right track.
Here’s what worked for my issue:
add_action('init', 'cpt_rewrite');
function cpt_rewrite(){
$args = array(
'public' => true,
'_builtin' => false,
);
$post_types = get_post_types( $args );
if ( $post_types ) {
foreach ( $post_types as $post_type ) {
add_rewrite_rule('^'.$post_type.'/([0-9]{4})/([0-9]{2})/?','index.php?post_type=".$post_type."&year=$matches[1]&monthnum=$matches[2]','top');
add_rewrite_rule('^'.$post_type.'/([0-9]{4})/?','index.php?post_type=".$post_type."&year=$matches[1]','top');
}
}
}
I used get_post_types() to get all my registered custom post types and then loop through each of them.
Related Posts:
- Rewrite custom post type url’s adding meta box values
- add_rewrite_endpoint() and Custom Post Type Archive
- CPT custom archive template does not get loaded
- Canonical url differs from custom post type archive URLs
- Add parent post to Custom Post Type [closed]
- Custom Post Type Date Based Archive URL rewrite [duplicate]
- Pages not appearing?
- Rewrite rules causing hole
- Custom archive URL as a subfolder of the custom post type slug
- Remove date rewrite rule for custom post type archive page
- Rewrite taxonomy permalink appended to CPT archive url
- How to hyperlink content from archive page with custom post-type
- Set thumbnail and title for a post type archive (not post itself!)
- How to set up Custom Post Type archive page as Front page
- Url to archive page for custom post type
- How can I use archive-{post_type}.php theme template?
- How to display CPT archives?
- Custom meta Title for custom post type archive from page
- Excluding a Custom Post Type with a specific tag using pre_get_posts
- Return the name of the post type
- custom post type archive page has 404 error even though has_archive is true
- Custom post type paging 404 error
- current-menu-item not added on page showing custom post type (rendered with archive template)
- Alphabetically sort a taxonomy.php template by post title
- Custom Permalinks for Custom Post Types
- Postname on unique permalink structure appends “-2” for a custom post type. How can I get this to stop happening?
- Custom Post Type posts, conflict with posts after add_rewrite_rule to top
- Custom Permalinks For CPT and pages with parent. Advanced WordPress
- How to integrate single and archive templates for custom post type in any WordPress theme
- Combine multiple CPT names to create valid permalinks
- Permalink problems with custom post type and custom taxonomy
- sort CPT on Custom Taxonomy Archive page
- Custom Post Type – Rewite Archive page
- SEO Concerns With Rewrite Rules
- Set parent for custom post type archive rewrite url
- Route subpage to Custom Post Type
- ACF for custom post type archive pages: which hook to use?
- Use the custom post type archive for taxonomies?
- How to rewrite custom taxonomy term archive to use THE SAME SLUG/front as CPT
- Custom Post type date archive for custom taxonomy
- Show categories of custom post type and not all posts
- Rewrite permalink for multiple post types (CPT) with taxonomy term
- CPT Archive pre_get_posts not working?
- WordPress custom permalink and archive link for custom post type
- How to rewrite the post slug of a custom post type post
- adding custom post type “name” to single template
- rewriterule not working
- Removed custom post type archive page shows blog page
- Custom Post Type Archive template not being called
- Custom Post Type with Configurable Sidebar via ACF
- Pagination for custom post types – url rewriting
- Query Posts that have Custom Taxonomy
- Plain links in single-class.php and archive-class.php not working
- Custom Post Type with modified permalink structure results in 404
- Modifying rewrite rules to use /category//
- Custom Post Type Custom Archive Page Not Working
- Custom Endpoint For Custom Post Type from Child Theme
- Getting custom taxonomy posts on archive page
- Custom post types and custom variables — add_rewrite_tag() not working
- Add Custom Values to Permalinks Through Custom Fields and Posts
- Why do I need to register my custom post type a second time when flushing rewrite rules?
- add_action ‘init’ from inside a class for custom post types
- Archive page for custom post type and custom taxonomy
- Custom post type archive – error in nav-menu-template.php
- Order Custom Post Type Archive by multiple values in functions.php
- Custom post type category permalinks and archive pages
- Make Next and Previous on single-$posttype.php use the same order as archive-$posttype.php
- Pagination does not work on Custom Post Type archive while trying to get load more function working
- Custom post rewrite rule not working
- CPT archive page – show one post from each taxonomy term
- Subpages Permalinks Issues
- WordPress custom taxonomy page
- Get single posts archive information
- How to have multiple archive pages?
- Displaying custom taxonomy menu in custom post type archive
- Create a Custom Path to Archive
- rewrite rules hierarchical
- Specifying a template for custom post type pages
- Rewrite rules for custom post type
- Custom taxonomy and post type rewrite rules
- WordPress sitemap with Custom Post Types
- Is possible register two archive pages for single custom post type?
- WordPress rewrite rule doesn’t work unless post category is explicitly called in regex
- Url Rewriting a dynamic wordpress page with parameters
- How to make a post accessible from multiple post type slugs?
- CPT archive with WP native/core tax/categories
- Remove cpt slug from url and use custom taxonomy instead breaks all other cpt
- How to set a ‘page’ as parent of a custom post type?
- Display All Courses in Course Archive Page
- Archive page of CPT’s custom Taxonomy
- Getting Same Description in All the Custom Taxonomy Posts
- Invalid content when I try to import custom post type from the old template wordpress
- Archive page for WordPress Custom Post Type doesn’t show pagination from paginate_links()
- 2 Custom Post Types In 1 Archive Page?
- multiple URLS for the same page
- Rewrite URL for CPT to domain/post-name/post-id
- Custom post type archive URLs with a single taxonomy
- Custom Post Type add_rewrite_rule
- How to apply multiple rewrite_rule to a single custom post type url?
- Different number posts per page based on custom post type term id