I'm trying to set up a redirect for drafted posts of the post type "Personnel", as we'd rather show a page with a custom message than a 404 page in this instance. I have added the following to functions.php
(adopted from this older thread) which for some reason only works for logged-in users- otherwise, the general 404 page shows. I've also tried adding the redirect to the single-personnel.php
template, but it has no effect. I'm wondering how to get this redirect working for all users (logged in or not) and/or if there's a better way to implement the redirect? Thanks for any insight here.
add_action( 'template_redirect', 'inactive_personnel_redirect', 0 );
function inactive_personnel_redirect() {
global $post;
if( ( $post->post_status == 'draft' ) && ( is_singular('personnel') ) ) {
wp_redirect( home_url() . '/about-us/inactive', 301 );
exit;
}
}