How can I only show certain posts?

The question:

How can I make it so only members see post summaries on category pages? Currently only members can access them, but they still show up on category pages for non members.

Note: I now made a plugin for this, you can get it at: https://github.com/NerdOfLinux/MemberOnly

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I haven’t had time to test this, but you can probably do this with the the_content filter hook. This way you won’t have to insert this code into archive.php, category.php, tag.php, author.php and any other archive file depending on how your theme is structured.

In your theme’s functions.php file add the following:

function my_filter( $content ) {

    $categories = array(
        'news',
        'opinions',
        'sports',
        'other',
    );

    if ( in_category( $categories ) ) {
        if ( is_logged_in() ) {
            return $content;
        } else {
            $content = '<p>Sorry, this post is only available to members</p>';
            return $content;
        }
    } else {
        return $content;
    }
}
add_filter( 'the_content', 'my_filter' );

So what this could should do is intercept the content of the post between when it’s pulled from the database and it’s displayed. Then it’s going to check and see if the post has one of the categories listed in the $categories array (NOTE: It’s best to use the category slug here in the array). Then it will see if the user is logged in.

If they are, then it will display the post. If they are not, then it will set the content to the message and then send that back in place of the post’s content.

If the user is logged in, or the post is not in any of those categories in the array, then it will return the post as normal.

NOTE: If you’re using the_excerpt() in your theme instead of the_content(), then simply change the hook in the add_filter() call to 'the_excerpt' and it should work.

Method 2

For this kind of thing, I prefer to work within templates rather than in functions.php. This way, you don’t need an additional if statement to determine what category you are applying the conditions to. You can apply this method to all categories using the category.php template or create custom templates for individual categories if you want to display different content in different categories. Personally, I find using templates for this kind of work makes it easier see what I’m doing.

In your case, you wou create an if statement like the example above with the is_user_logged_in tag before whatever your want to reveal to your members. Then add an else for whatever you want to display instead when a user is not logged in before closing the statement.

<?php if ( is_user_logged_in() ) { 
    the_excerpt(); // or whatever
} else {
    echo '<p>This information is for members only.</p>'; // or whatever 
}?>

Method 3

You can use the post id’s store them in an array and then display them one by one.

  <?php $thePostIdArray = array("28","74", "82", "92"); ?>
  <?php $limit = 4 ?>
  <?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); $counter++; ?>
  <?php if ( $counter < $limit + 1 ): ?>
  <div class="post" id="post-<?php the_ID(); ?>">
  <?php $post_id = $thePostIdArray[$counter-1]; ?>
  <?php $queried_post = get_post($post_id); ?>
  <h2><?php echo $queried_post->post_title; ?></h2>
  </div>
  <?php endif; ?>
  <?php endwhile; ?>
  <?php endif; ?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment