How to Get All Posts but the Private ones?

I am creating a frontend mosaic view where i need to show the most recent posts. So i need to show all published posts to every visitor, but keep the private ones for the logged in users.

I know how to add the private posts, but I don’t find a way to remove them from the query.

(Removing them from the results would work but, then, the number of posts on the page will vary depending on how many recent posts are private: this is not what I want!)

(As a workaround, I can refrain from publishing the private posts and showing the private on top of the published for the members…)

Any idea?

1 Answer
1

I believe get_posts returns all published (no-private posts) but does display password protected posts. In fact, by default, the standard WP_Query loop does this as well. So by default, private posts shouldn’t appear.

If you wanted to display private posts for those logged in, and not those who are logged out, you could use the permissions parameter perm=>'readable'

$wp_query = new WP_Query( array( 'perm'=>'readable' ) );

This returns any posts the current user has permission to read. If they are logged out, then they only have permission to read published, non-private posts. Only those logged in, with the ability to read private posts will see the private posts.

Note

Password protected posts will still appear, but will obviously require a password to read the posts.

Leave a Comment