How do I redirect all 404 errors of a specific post type to another URL?

It looks like template_redirect is as far up the WordPress action chain you can go while still being able to detect that a 404 error is being thrown. This action will trigger before any template files would be loaded so it will happen before loading unnecessary resources.

You can try adding this into your /wp-content/themes/yourtheme/functions.php file to achieve a dynamic redirect for all 404’s that happen when viewing single jobs:

add_action( 'template_redirect', 'unlisted_jobs_redirect' );
function unlisted_jobs_redirect()
{
    // check if is a 404 error, and it's on your jobs custom post type
    if( is_404() && is_singular('your-job-custom-post-type') )
    {
        // then redirect to yourdomain.com/jobs/
        wp_redirect( home_url( '/jobs/' ) );
        exit();
    }
}

Leave a Comment