admin管理员组

文章数量:1279117

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?

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 asked Dec 30, 2011 at 13:38 DuveitDuveit 831 gold badge1 silver badge3 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

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.

/* 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;
                $wp_query->set_404();
                status_header(404);
                nocache_headers();
            }
        }
    }

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() ); 
      exit();
    }    
  }

  add_action("template_redirect","custom_redirect");

本文标签: Making custom post type visible for only logged in users