Add session check to posts

If you dont want to completely block your users from logging in another good option is to use the "is_within_session_limit" filter to add a session check to any post.

add_action("template_redirect", function() {
    // Array with post IDs
    $posts_to_block = array(1,2);

    // Check if current posts is one to protect
    if (!in_array(get_the_ID(), $posts_to_block) ) {
        return;
    }

    $within_limit = apply_filters("is_within_session_limit", true, wp_get_current_user());
    if (!$within_limit) {

        // If referrer could be determinde redirect there else home url
        $url = wp_get_referer() === false ? home_url() : wp_get_referer();

        wp_safe_redirect($url);
        exit;
    }
});

Explaination of this snippet

  1. The variable "posts_to_block" contains an array of post ids to check the session limit for
  2. Check if the id of the current post is within the "posts_to_block" variable
  3. Apply the "is_within_session_limit" filter to check if the current user is within the session limit
  4. If not the user gets redirected to the referring page.
    1. If the referring page can not be determined the user gets redirected to the home page