How to redirect 404 of custom posts type in WordPress

redirect 404 of custom posts type in WordPress

You can redirect 404 of  a custom post to a specific page by following snippet.

 

add_action( 'template_redirect', 'your_post_redirect' );

function your_post_redirect()
{
    // check if is a 404 error, and on your custom post type
    if( is_404() && is_singular('your-custom-post-type') )
    {
        // then redirect to yoursite.com/some-page/
        wp_redirect( home_url( '/some-page/' ) );
        exit();
    }
}

 

Related Posts