Advertisement
Guest User

Untitled

a guest
Aug 10th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. // Get all posts with the access level of 'Member'
  2. function members_get_member_posts() {
  3.     $post_ids = array();
  4.     $args=array(
  5.         'post_type' => array('post','tribe_events','document'),
  6.         'meta_key' => '_members_access_role',
  7.         'meta_value' => 'member',
  8.         'post_status' => array('publish','private')
  9.     );
  10.     $protected_posts = get_posts($args);
  11.     if($protected_posts) {
  12.         foreach($protected_posts as $p) {
  13.             $post_ids[] = $p->ID;
  14.         }
  15.     }
  16.     // return an array of paid post IDs
  17.     return $post_ids;
  18. }
  19.  
  20. // Hide all posts from users who are not logged-in or are not administrators or members
  21. function members_hide_member_posts($query) {
  22.     $current_user = wp_get_current_user();
  23.     if(empty($current_user) || ($current_user->roles[0] != 'member' && $current_user->roles[0] != 'administrator') && (false == $query->query_vars['suppress_filters'])) {
  24.         $protected_posts = members_get_member_posts();
  25.         if($protected_posts)
  26.             $query->set('post__not_in', $protected_posts);
  27.     }
  28.     return $query;
  29. }
  30. add_filter('pre_get_posts', 'members_hide_member_posts');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement