How can we set up a redirection for old URLs to new ones in a WordPress website to avoid error messages?

To set up a redirection for old URLs to new ones in a WordPress website and avoid error messages, you can use the "redirect_canonical" filter hook in your theme's functions.php file. This filter allows you to check and modify the URL before WordPress performs its canonical URL redirection.

function custom_redirect_canonical($redirect_url, $requested_url) {
    if ($requested_url == 'OLD_URL') {
        return 'NEW_URL';
    }
    return $redirect_url;
}
add_filter('redirect_canonical', 'custom_redirect_canonical', 10, 2);