Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########
- // restrikcija posameznih kategorij za grupo ki je subscriber
- // source: https://wordpress.stackexchange.com/questions/113500/only-show-category-to-certain-user-levels-without-plugin
- add_filter('template_include', 'restict_by_category');
- function check_user() {
- $user = wp_get_current_user();
- $restricted_groups = array('company1', 'company2', 'company3', 'subscriber'); // categories subscribers cannot see
- if ( ! $user->ID || array_intersect( $restricted_groups, $user->roles ) ) {
- // user is not logged or is a subscriber
- return false;
- }
- return true;
- }
- function restict_by_category( $template ) {
- if ( ! is_main_query() ) return $template; // only affect main query.
- $allow = true;
- $private_categories = array('podjetje', 'personal', 'nekategorizirano', 'razno', 'sola-2'); // categories subscribers cannot see
- if ( is_single() ) {
- $cats = wp_get_object_terms( get_queried_object()->ID, 'category', array('fields' => 'slugs') ); // get the categories associated to the required post
- if ( array_intersect( $private_categories, $cats ) ) {
- // post has a reserved category, let's check user
- $allow = check_user();
- }
- } elseif ( is_tax('category', $private_categories) ) {
- // the archive for one of private categories is required, let's check user
- $allow = check_user();
- }
- // if allowed include the required template, otherwise include the 'not-allowed' one
- return $allow ? $template : get_home_url();//get_template_directory() . '/not-allowed.php';
- }
- ###########
- add_filter( 'pre_get_posts', 'hide_private_cats', 10);
- function hide_private_cats($query) {
- if (is_admin() || check_user()) {
- return $query; // If this is an admin page or your user check passes, do nothing
- }
- $query->set('category__not_in', array( 3, 8, 10 )); // don't show posts that are in the categories specified (only takes IDs)
- return $query;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement