I'm making a custom post type which is intended to be viewed by logged in users only, I've created a single-{postType}.php to override the default rendering and has included a logincheck in the top which redirects to the login page and back, after login.

I've not enabled archive for my post type, so I imagine I don't have to override the custom archive page in addition.

And this seems to work as intended, and easy maintainable without any plugins or whatnot.

But is this enough to make sure the posts won't be visible for unauthorized users? What possible viewmodes/url-paths does a custom post type have?

Afaik, I have to make the post publicly_queryable = TRUE, as I want it to be visible on the front end, although, only for logged in users. I'm not sure if there is some trickery I can do with the settings for the custom post type, or specific queries I may use in the single-{postType}.php to fetch non public queryable posts?

4 Answers 4


A simple filter on your post content can do this job easily. Lets try this code

function tp_stop_guestes( $content ) {
    global $post;

    if ( $post->post_type == 'YOUR_CUSTOM_POSTTYPE' ) {
        if ( !is_user_logged_in() ) {
            $content = 'Please login to view this post';

    return $content;

add_filter( 'the_content', 'tp_stop_guestes' );

We are applying filter on the post content. If the post type is your custom post type and the user is not logged in, s/he will see "Please login to view this post" instead of original content.

  • 2
    How could this be set up to cause a normal 404 (not a visible redirect to mydomain.com/404) for non-logged in users?
    – supertrue
    Commented Mar 9, 2012 at 22:15
/* Making custom post type mailer visible for only logged in users */
    add_action('template_redirect', 'mailer_404_loggedin_func');

    function mailer_404_loggedin_func() {
        global $post;
        if ($post->post_type == 'mailer') {
            if (!is_user_logged_in()) {
                global $wp_query;
                $wp_query->posts = [];
                $wp_query->post = null;

You could also extend the function above into a shortcode that would allow you to specify which parts of a post you would want hidden from users who are not logged-in.

function custom_redirect() {        
    global $post;

    if ( $post->post_type == 'journals' || $post->post_type == 'projects' || $post->post_type == 'tribe_events' && ! is_user_logged_in() ) {
      wp_redirect( home_url() ); 


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.