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
- Where, When, & How to Properly Flush Rewrite Rules Within the Scope of a Plugin?
- Page Template as Custom Post Type Archive
- Can’t get post id on page that is a custom post type archive
- Proper way of making custom post type landing page or archive page
- Is it possible to remove the posts_per_page limit on a specific post type?
- Is it possible to have an index page for taxonomy term for each custom post type it is assigned to?
- How to Add Pages Under Custom Post Type URL Structure?
- How to get_queried_object on multiple objects?
- Assign Page Template Within A Custom Post Type
- Permalinks using event date (year & month) instead of publication date
- Posts per Page on custom Taxonomy Template
- WP Rewrite Rules – Custom post type & taxonomy
- Remove base slug in CPT & CT, use CT in permalink
- Custom Post Type – Archive page title
- Getting taxonomy terms used by custom post type
- Archive or taxonomy pages not working for custom post type
- Disabling post_type in URL
- single-{post_type}.php is not loaded despite flushing rules
- Custom Post Type rewrite rules not working, how to alter the rewrite order?
- Custom Permalink for Taxonimie and Custom Post Type
- Rewrite numeric ID parameter for hierarchical custom post type
- Adding the taxonomy before the custom post type
- taxonomy template for custom post type the same as the archive
- Loop that displays the first post of every available custom post type?
- Conditional tags don’t work
- Permalink Structure CPT With Custom Taxonomy Fails
- Specify a Page as the parent to the CPT Archive
- Making a custom help center page
- Remove url rewrites for registered taxonomies
- Unable to get post archive link
- Am I using the flush_rewrite_rules function in the right place?
- Custom Post Type archive loop separated by term, in specific term order
- Show only posts from one category on custom post type archive page
- How should I structure my post types?
- Can’t get custom rewrite tag, query var, permastruct (permalink structure), and rewrite rule to work properly together
- Display custom post type category, while in a CPT category, then the posts beneath
- Customize Custom Post Type URL
- Number of Custom Post Types published are not being shown in the custom page
- Post Type rewrite rule to point to custom Page/template
- Rewrite rule for Custom post type Monthly and Yearly archive
- Add a permalink variable onto custom post type URL after post name slug
- Custom post type archive with page as parent url
- Set featured image to archive.php
- How to query 2 custom post types that need to share a slug?
- Custom post type, global categories — what’s the template name?
- Url to archive page for custom post type
- How can I use archive-{post_type}.php theme template?
- Custom meta Title for custom post type archive from page
- Return the name of the post type
- Alphabetically sort a taxonomy.php template by post title
- Set parent for custom post type archive rewrite url
- adding custom post type “name” to single template
- Custom Post Type Archive template not being called
- Custom Post Type Custom Archive Page Not Working
- Custom Endpoint For Custom Post Type from Child Theme
- Getting custom taxonomy posts on archive page
- WordPress custom taxonomy page
- Displaying custom taxonomy menu in custom post type archive
- Is possible register two archive pages for single custom post type?
- Remove cpt slug from url and use custom taxonomy instead breaks all other cpt
- How to properly set the rewrite rules for this case?
- Static Front Page not working for custom theme
- How to fix 404 on post after prepending Custom Post Type url with Custom Taxonomy Term slug
- Display Custom Post Type Archive content with Shortcode
- Redirect Custom Post Type from Child Taxonomies
- Unsure what page hierarchy to use with CPT UI to show on front end
- The page or ad listing you are trying to reach no longer exists or has expired
- Only show categories that have posts within custom post type
- Meta_query by date for Events archive
- Custom post type rewrite and wp_pagenavi interfering with each other
- How to Show all Values in category.php page using post_type
- Custom post type archive page not showing on archive-posttype.php
- Custom Post Type rewrite url to /category/pagetitle [duplicate]
- Making a archive gallery page for a custom post type which already has an archive page
- redirect old post type url to new structure url
- Templates list in “Page Attributes” metabox is inaccurate
- Stop Custom post type from being searched via URL
- Multiple Custom Post Type permalink issue
- Custom Post Types and rewrite rules – One CPT works and a cloned one doesn’t
- Archive page for a custom post type using WPML
- How to rewrite CPT-Permalinks the correct way (incorporating meta-box-data)?
- title tag for custom post type remove taxonomy name from title tag
- How can I retrieve the slug for a custom post type via name?
- What is the proper way implement a global $wp_rewrite?
- rewrite rules add a folder in the path
- Display results from two Custom Post Types in page template
- Ideal top-level slug methods
- Custom Taxonomy in Permalink from post type
- Pagination not working for archive
- 404 Error On Custom Taxonomy Pages 2, 3, etc
- CPT Archive Pagination – Page not found