The post_rewrite_rules WordPress PHP filter allows you to modify the rewrite rules used for “post” archives.
Usage
add_filter('post_rewrite_rules', 'your_custom_function_name'); function your_custom_function_name($post_rewrite) { // your custom code here return $post_rewrite; }
Parameters
- $post_rewrite (string[]): Array of rewrite rules for posts, keyed by their regex pattern.
More information
See WordPress Developer Resources: post_rewrite_rules
Examples
Add a custom rule to post rewrite rules
Add a custom rewrite rule to redirect post URLs containing the word “redirect” to the homepage.
add_filter('post_rewrite_rules', 'add_custom_redirect_rule'); function add_custom_redirect_rule($post_rewrite) { $post_rewrite['(.?.+?)/redirect/(.+)$'] = 'index.php'; return $post_rewrite; }
Remove year from post rewrite rules
Remove the year from post rewrite rules, so the URL structure becomes “/month/day/post-name/”.
add_filter('post_rewrite_rules', 'remove_year_from_post_rewrite_rules'); function remove_year_from_post_rewrite_rules($post_rewrite) { $post_rewrite['([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?monthnum=$matches[1]&day=$matches[2]&name=$matches[3]&page=$matches[4]'; return $post_rewrite; }
Add a prefix to post rewrite rules
Add a prefix to post rewrite rules, so the URL structure becomes “/blog/year/month/day/post-name/”.
add_filter('post_rewrite_rules', 'add_prefix_to_post_rewrite_rules'); function add_prefix_to_post_rewrite_rules($post_rewrite) { $post_rewrite['blog/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]'; return $post_rewrite; }
Remove category from post rewrite rules
Remove the category from post rewrite rules, so the URL structure becomes “/year/month/day/post-name/”.
add_filter('post_rewrite_rules', 'remove_category_from_post_rewrite_rules'); function remove_category_from_post_rewrite_rules($post_rewrite) { $post_rewrite['([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]'; return $post_rewrite; }
Add custom post type to post rewrite rules
Add a custom post type “portfolio” to post rewrite rules, so the URL structure becomes “/portfolio/year/month/day/post-name/”.
add_filter('post_rewrite_rules', 'add_custom_post_type_to_post_rewrite_rules'); function add_custom_post_type_to_post_rewrite_rules($post_rewrite) { $post_rewrite['portfolio/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?post_type=portfolio&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]'; return $post_rewrite; }
Add author to post rewrite rules
Add the author name to post rewrite rules, so the URL structure becomes “/author/year/month/day/post-name/”.
add_filter('post_rewrite_rules', 'add_author_to_post_rewrite_rules'); function add_author_to_post_rewrite_rules($post_rewrite) { $post_rewrite['([^/]+)/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&name=$matches[5]&page=$matches[6]'; return $post_rewrite; }
Change post pagination structure
Change the post pagination structure, so the URL for paginated posts becomes “/year/month/day/post-name/p/2/”.
add_filter('post_rewrite_rules', 'change_post_pagination_structure'); function change_post_pagination_structure($post_rewrite) { $post_rewrite['([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/p/([0-9]+)?/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]'; return $post_rewrite; }