Skip to content

Instantly share code, notes, and snippets.

@mattwiebe
Last active December 15, 2015 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattwiebe/5302207 to your computer and use it in GitHub Desktop.
Save mattwiebe/5302207 to your computer and use it in GitHub Desktop.
Filter out posts according to permissions and categories
<?php
add_action( 'pre_get_posts', 'mw_post_limiter' );
function mw_post_limiter( $wp_query ) {
// only modify for the main query - if we're not there, bail.
if ( ! $wp_query->is_main_query() )
return;
// category_slug => permission
$filtration_pairs = array(
'friend' => 'view_friend_content',
'inner-circle' => 'view_inner_content',
'family' => 'view_family_content'
);
$not_in = array();
foreach( $filtration_pairs as $cat_slug => $permission ) {
if ( current_user_can( $permission ) )
continue;
$not_in[] = $cat_slug;
}
// if we made it through the above without tripping any permissions, just bail
if ( empty( $not_in ) )
return;
// first grab any tax_query that might already be set. being a good citizen and stuff.
$tax_query = (array) $wp_query->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $not_in,
'operator' => 'NOT IN'
);
// now set it
$wp_query->set( 'tax_query', $tax_query );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment