Trying to add an action to redirect based on if user is not logged in and is accessing certain post types. Problem is, during template_redirect, $post
is NULL and other ones the headers seem to have already been sent so it won't redirect. What would be the appropriate action to use here?
add_action( 'template_redirect', 'mytheme_restrict_user_content' );
function mytheme_restrict_user_content(){
if (!is_user_logged_in() ) {
$restrict = false;
$restricted_post_types = array('documentlibrary', 'events');
if ( in_array( $post->post_type, $restricted_post_types ) ){
$restrict = true;
}
if ($restrict){
auth_redirect();
}
}
}