Advertisement
brook-tribe

Selectively hide events from specific user roles

Jan 28th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. // Snippet now hosted here: https://gist.github.com/elimn/6a57feaec4bd09db9923
  2.  
  3. add_filter('pre_get_posts', 'event_cat_permissions');
  4.  
  5. function event_cat_permissions ($wp_query) {
  6.  
  7.         // Include all posts on admin views
  8.         if ( is_admin() ) return $wp_query;
  9.  
  10.  
  11.         $exclude_cats = array();
  12.  
  13.         // Exclude cats if not admin... copy/modify this logic for each custom role
  14.         if (!current_user_can('administrator')) {
  15.                 $exclude_cats[] = 'exclude-slug';
  16.         }
  17.  
  18.  
  19.         // Join with current tax query if set
  20.         if (is_array($wp_query->tax_query))
  21.                 $tax_query = $wp_query->tax_query;
  22.         else
  23.                 $tax_query = array();
  24.  
  25.  
  26.         // Setup an exclude from the tribe_events_cat taxonomy
  27.         $tax_query[] = array(
  28.                 'taxonomy'  => 'tribe_events_cat',
  29.                 'field'     => 'slug',
  30.                 'terms'     => $exclude_cats,
  31.                 'operator'  => 'NOT IN'
  32.         );
  33.  
  34.         $wp_query->set('tax_query', $tax_query);
  35.  
  36.         return $wp_query;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement