I have a custom post type (audits). I am trying to make the each post only viewable in the frontend by the post author, and the administrator. So, essentially a private post only for logged in users that match the post author id and admin.
I’ve seen many answers for how to restrict the posts in the admin dashboard, but none for front end, since most posts are usually public.
Any help is greatly appreciated!
I would say that the approach depends on what you want the user to see if they are denied access to the post. Would you want to display a message saying you cannot view this post? Or throw a 404?
If you wanted to throw a 404, you could use the
template_redirect
action hook.If you wanted to display a message to the user, then you would simply run the exact same logic above on the actual single.php template and display an authorized message instead of the post title, content, etc.
Hope this points you in the right direction.