Code to Disable “Year Archives” in WordPress

/* Disable Year Archives WordPress, Redirects to 404 */
add_action('template_redirect', 'wpflt_remove_wp_year_archives');
function wpflt_remove_wp_year_archives(){
  if( is_date() ) {
    global $wp_query;
    $wp_query->set_404();
  }
}

WordPress Year Archive URL: https://example.com/2022/07/

Above code to functions.php redirects WordPress Year Archive URL to 404 Page. If you want to redirect Year Archive URL to Homepage, use the code below.

/* Disable Year Archives WordPress, Redirects to Homepage */
add_action('template_redirect', 'wpflt_remove_wp_year_archives');
function wpflt_remove_wp_year_archives(){
  if( is_date() ) {
        $target = get_option('siteurl');
        $status = '301';
        wp_redirect($target, 301);
        die();
  }
}