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' );
Related Posts:
- Meta_Query as a way how to setup CPT permalinks – is it a good thing?
- How to filter custom post type archive by meta value
- Custom post type permalink structure with the category also acting as an archive
- Rewrite custom post type url’s adding meta box values
- Bulk 301 redirect for custom post type
- Custom query variable – get wordpress to redirect to nice permalink url
- Redirecting when changing custom post type slugs?
- Query var removed after rewrite
- CPT Archive with core Category
- Ignoring slug capitalization on rewrite rule for custom post type archive page
- Custom post type archive with page as parent url
- rewriterule not working
- Archive page for custom post type and custom taxonomy
- Custom Post Type Date Based Archive URL rewrite [duplicate]
- How to change URL for custom post type archive?
- Pretty Permalinks with CTP and Taxonomies / Hierarchical
- Set archive of custom post type like ‘posts’ page in reading settings is set
- Custom Post Type canonical link / pagination redirecting to root
- Show index and not the archive for “Dog Custom Post” with default slug
- Custom archive URL as a subfolder of the custom post type slug
- Can’t get post id on page that is a custom post type archive
- Is it possible to change the URL of custom post types to hide the post type slug?
- 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?
- Posts per Page on custom Taxonomy Template
- Display CPT taxonomies as an archive page
- Including two taxonomies in a permalink structure
- WP Rewrite Rules – Custom post type & taxonomy
- URL Design for Sub-Posts?
- Remove base slug in CPT & CT, use CT in permalink
- How do I list terms of a custom taxonomy at i.e. domain.com/brands/
- Permalink Structure problem with cpt and custom taxonomy
- How can I fix a slow redirect after form submit from frontend (no plugin)?
- Getting taxonomy terms used by custom post type
- Custom Taxonomy 404
- 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
- Loop that displays the first post of every available custom post type?
- Different permalink for CPT and regular Posts/Pages but why?
- In WordPress how do you create a custom post type with the author’s name in the slug?
- Custom Post Type without an archive page
- How can I display an archive of only one category of my custom post type?
- Permalink Structure CPT With Custom Taxonomy Fails
- Conditionally custom post type url rewrite
- Specify a Page as the parent to the CPT Archive
- Redirect page to first post in custom post type
- Making a custom help center page
- Unable to get post archive link
- 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?
- 301 redirect for converted post type
- Child post with numeric only slug keeps redirecting to parent
- 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
- WordPress custom post type permalink rewrite shows page not found
- Customize Custom Post Type URL
- Custom Post Type and Category Base Rewrite Issue
- Redirect custom post type category to other custom post type category
- Rewrite URL of Custom Post Type for Jobs
- Custom permalink with child taxonomy terms
- Pagination custom post type not working with rewrite slug
- Custom post type, global categories — what’s the template name?
- Url to archive page for custom post type
- Return the name of the post type
- After creating Custom post type by user delete old one
- Set parent for custom post type archive rewrite url
- URL Rewrite – Page Archive
- adding custom post type “name” to single template
- Urls in Custom Post Type work for Terms but not for its Taxonomies
- Custom Post Type Archive template not being called
- Displaying custom taxonomy menu in custom post type archive
- custom post archive URL is wrong
- Is possible register two archive pages for single custom post type?
- WordPress is ignoring my post rewrite rule
- Custom taxonomy permalink without term, redirection to CPT slug
- Custom Permalink For Custom Post Type – Working Fine But Broken For Pagination
- Redirect Existing Post to CPT
- Duplicate content problems after adding an archive page to the article’s post type
- Static Front Page not working for custom theme
- Redirect Custom Post Type from Child Taxonomies
- Only show categories that have posts within custom post type
- Meta_query by date for Events archive
- Custom 404 redirect for a luddite
- Custom post type rewrite and wp_pagenavi interfering with each other
- Redirect sub-page URLs to parent without changing URL
- Custom post type archive page not showing on archive-posttype.php
- 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
- Why is a custom post type’s URL “/?cposts=name-of-the-post” but default post’s URL is “/?p=ID”?
- Redirect Custom Post Type after user submits update
- title tag for custom post type remove taxonomy name from title tag
- How can you preserve URLs when moving posts to a custom post type?
- Custom Post Type Base URL
- Custom Post Type and single-posttype template
- Pagination not working for archive
- Menu Structure and URL structure, with Pages and Post-Type-Posts