I'm building my friends blog and he asked me a rather odd request. He wants the latest five blog posts to be visible to non-registered members but not to others. I'm using s2Member for membership handling.

My Initial attempt was just limiting the number of blog posts to 5 and hiding the pagination button based on if someone's logged in and of the right user level. This works fine, but people can manually add ?page_id=x&paged=y to view more content. Also, if they know the name of the post, they can view it as well.

My thoughts were to add some sort of auto-categorizing, where if the posts aren't in the 5 newest, they would be auto tagged with the category "archive" which then can be protected using s2members built-in functionality. I just have no clue where to begin with auto tagging a category.

I would appreciate any insight on where to begin with all of this? Also, if you have a better idea of how to do something similar, I'm open to suggestions.

Thanks in advance!


2 Answers 2


Before the start of your loop on single.php, add this code to check the current post against the fifth most recent post date. If the current post is more recent than fifth, show the post, and if older, show a message that has to do with being loggedin/a member.

global $post;
$tmp_post = $post; // Retain current post query

$fifth_post = ''; // Set variable

// Query for fifth post date
$args = array( 'numberposts' => 1, 'offset'=> 4 );
$fifthPost = get_posts( $args );
foreach( $fifthPost as $fifth ) : setup_postdata($fifth);
$fifth_post = get_the_date("Y-m-d");

// Restore current post
$post = $tmp_post;

$fifth_post = strtotime($fifth_post);
$this_post = date("Y-m-d");
$this_post = strtotime($this_post);

// Post is one of most recent 5, OR user is logged in
if ( $this_post > $fifth_post || is_user_logged_in() ) {
    // Begin regular loop
} else if (!is_user_logged_in() && $this_post < $fifth_post) {
    // Show whatever message to non-loggedin/non-members
  • Thanks a bunch Steph. Been a bit slammed, I'll give a look at this code after I have dinner. Thanks in advance!
    – tr3online
    Jan 20, 2012 at 3:42
  • Ah, very nice code. I think this will work great for locking people out of specific posts. How about locking people out from seeing more than 5 on the home.php? I can always hide the pagination based on if the user is logged in or not, but that wont stop people from going to paged=x and seeing the titles of the post. Ideally, I guess I'd like to lock down both
    – tr3online
    Jan 20, 2012 at 4:02
  • You can place this before the loop on any page, really. So when it is paged, it will just display your message in place of each post if they are not loggedin/a member. If this is the best answer, don't forget to mark it :)
    – 5t3ph
    Jan 20, 2012 at 14:19

You could generate an array of the postids for the first five posts (any query method will do), then use that to restrict access to everything but them by something like:

 if( !in_array( get_the_ID(), $first_five_array ) && !is_user_logged_in() )
     wp_redirect( $location, 307);

The one major downside that I see to this is that search engines will piss themselves trying to index his content...you might want to make some sort of provision that allows search engines to parse more than just the first 5 posts.

  • I was thinking that as well. That and if he offers content, that is eventually locked, it'll get indexed and cached so people can see it anyways, right? I think he just wants to use it as a gimmick to get people to become a member, or something. I told him it was odd when he requested it. Any ideas how to fix the indexing problem? Thanks!
    – tr3online
    Jan 20, 2012 at 3:42
  • The easiest way to fix the indexing problem is going to be to look for the search bots and allow them access, I'm not 100% sure what the detection method is for search engine bots, but I know it exists...google around some. The content will be cached for a short period of time, but when it goes off the site, google will not consider it relevant to the site anymore as soon as it crawls and finds that it's not there. Realistically the best way to get people to sign up is by having compelling content that provokes comments.
    – mor7ifer
    Jan 20, 2012 at 10:00
  • don't forget to mark the question solved, btw, and maybe open a new question on how to deal with the idea of allowing the bots to see the site but not the users.
    – mor7ifer
    Jan 20, 2012 at 10:00

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.