Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks, this sounds promising, I'll try it out. –  Duveit Jan 3 '12 at 8:10
    
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 Mar 9 '12 at 22:15
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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