Reputation: 75
I know you can use WP_Query
if the user is logged in to return all the posts with a status of private
, but cannot see how to do this for a user who isn't logged in?
EG: For users who are logged in...
$query = new WP_Query( 'post_status=private' );
But, how to do for Users who are not logged in?
Upvotes: 0
Views: 262
Reputation: 26065
You can use the filter posts_clauses
, like:
add_filter( 'posts_clauses', 'wpse70214_posts_clauses' );
function wpse70214_posts_clauses( $pieces )
{
global $wpdb;
if( !is_user_logged_in() )
$pieces['where'] = str_replace(
"post_status = 'publish')",
"post_status = 'publish' OR $wpdb->posts.post_status = 'private') ",
$pieces['where']
);
return $pieces;
}
Upvotes: 1
Reputation: 2513
You could create a guest user, and if the current user is not logged in use code to log them in as a guest...then your query will work.
You will want to review groups and permissions when building out the solution. Also, the last thing to consider is how to present the login screen to guest users so that they can login as a real user.
Upvotes: 0