Preventing 404 error on empty date archive

You can move the code from OP’s answer into a 404 template filter and force WP to load the date.php file instead of 404.php. It’ll still send the 404 header, but render the page with a different template.

function wpd_date_404_template( $template="" ){
    global $wp_query;
    if( isset($wp_query->query['year'])
        || isset($wp_query->query['monthnum'])
        || isset($wp_query->query['day']) ){
            $template = locate_template( 'date.php', false );
    }
    return $template;
}
add_filter( '404_template', 'wpd_date_404_template' );

Leave a Comment