Redirect WordPress Custom Post Type

WordPress hook is a popular way to overwrite default function in WordPress. Perhaps you want to prevent non logged-in user to access your singular custom post which by default will be displayed using single.php file template. This is explained in WordPress template hierarchy. template_include hook can be used as one of the options available.

Here sample code to be included in your theme functions.php file. In this example, when non-logged in user trying to access the ‘mycustompost’ single post type, it will be displayed the 404 template.