Using WordPress ‘date_rewrite_rules’ PHP filter

The date_rewrite_rules WordPress PHP filter modifies the rewrite rules used for date archives. Date archives typically include patterns like /yyyy/, /yyyy/mm/, and /yyyy/mm/dd/.

Usage

add_filter('date_rewrite_rules', 'my_custom_date_rewrite_rules');
function my_custom_date_rewrite_rules($date_rewrite) {
    // your custom code here
    return $date_rewrite;
}

Parameters

  • $date_rewrite (string[]): Array of rewrite rules for date archives, keyed by their regex pattern.

More information

See WordPress Developer Resources: date_rewrite_rules

Examples

Remove day archive

Remove the day archive by unsetting the corresponding rewrite rule.

add_filter('date_rewrite_rules', 'remove_day_archive');
function remove_day_archive($date_rewrite) {
    unset($date_rewrite['([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$']);
    return $date_rewrite;
}

Add custom date format

Add a custom date format for date archives using the format /yyyy/mm-name/.

add_filter('date_rewrite_rules', 'custom_date_archive_format');
function custom_date_archive_format($date_rewrite) {
    $date_rewrite['([0-9]{4})/([0-9]{1,2})-([^/]+)/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]';
    return $date_rewrite;
}

Remove all default date archives

Remove all default date archives by returning an empty array.

add_filter('date_rewrite_rules', 'remove_all_date_archives');
function remove_all_date_archives($date_rewrite) {
    return [];
}

Change year archive format

Change the year archive format to /archive/yyyy/.

add_filter('date_rewrite_rules', 'change_year_archive_format');
function change_year_archive_format($date_rewrite) {
    $date_rewrite['archive/([0-9]{4})/?$'] = 'index.php?year=$matches[1]';
    return $date_rewrite;
}

Change month archive format

Change the month archive format to /yyyy-month/.

add_filter('date_rewrite_rules', 'change_month_archive_format');
function change_month_archive_format($date_rewrite) {
    $date_rewrite['([0-9]{4})-month/?$'] = 'index.php?year=$matches[1]&monthnum=$matches[2]';
    return $date_rewrite;
}