Making custom post type visible for only logged in users

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.

Leave a Comment