Last active
December 15, 2015 18:09
-
-
Save mattwiebe/5302207 to your computer and use it in GitHub Desktop.
Filter out posts according to permissions and categories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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