1

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;
    }
}
nickpish
  • 839
  • 4
  • 24
  • 43
  • I can't see why this hook should behave differently for logged-in vs non logged-in users. Do you have any caching plugins in use? – CBroe Sep 16 '22 at 07:39
  • @CBroe I don't have any caching plugins and have tried using multiple browsers and still the same result- it doesn't seem to make much sense. I've also tried using different hooks to attach the redirect, but doesn't help. – nickpish Sep 16 '22 at 14:06

0 Answers0