Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Get all posts with the access level of 'Member'
- function members_get_member_posts() {
- $post_ids = array();
- $args=array(
- 'post_type' => array('post','tribe_events','document'),
- 'meta_key' => '_members_access_role',
- 'meta_value' => 'member',
- 'post_status' => array('publish','private')
- );
- $protected_posts = get_posts($args);
- if($protected_posts) {
- foreach($protected_posts as $p) {
- $post_ids[] = $p->ID;
- }
- }
- // return an array of paid post IDs
- return $post_ids;
- }
- // Hide all posts from users who are not logged-in or are not administrators or members
- function members_hide_member_posts($query) {
- $current_user = wp_get_current_user();
- if(empty($current_user) || ($current_user->roles[0] != 'member' && $current_user->roles[0] != 'administrator') && (false == $query->query_vars['suppress_filters'])) {
- $protected_posts = members_get_member_posts();
- if($protected_posts)
- $query->set('post__not_in', $protected_posts);
- }
- return $query;
- }
- add_filter('pre_get_posts', 'members_hide_member_posts');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement