• Resolved Shahin

    (@shahinag)


    function exclude_cats() {
        if( !current_user_can('manage_options') ) {
            add_filter( 'list_terms_exclusions', 'exclude_args', 10, 2 );
        }
    }
    
    function exclude_args($exclusions,$args) {
        $current_user = wp_get_current_user();
    
        if (strpos($current_user->adi_title, 'Support') !== false) {
            $showcat = 26;
        } elseif (strpos($current_user->adi_title, 'Sales') !== false) {
            $showcat = 27;
        }
    
        return $exclusions . " AND ( t.term_id = " . $showcat . " )";
    }
    add_action( 'admin_init', 'exclude_cats' );

    I’ve got this code working for only showing the category the user is supposed to see/work with.

    But I want the subcategories to be shown too. Any ideas?

    Also, does this mean that whenever a contributor, that only sees/works with different categories, sends a post for approval – does that post show up for all editors or does it just show for those editors that can see the specific category (as in this code)?

Viewing 1 replies (of 1 total)
  • Thread Starter Shahin

    (@shahinag)

    Solved. Here’s the code:

    function exclude_args( $exclusions, $args ) {
        // get the list of terms our function has set as excludable
        global $my_plugin_exclusions;
    
        // if the exclude list is empty, we send everything back the way it came in
        if ( empty( $my_plugin_exclusions ) ) {
            return $exclusions;
        }
    
        // tell wordpress to exclude all the categories we discovered
        $exclusions .= " AND ( t.term_id NOT IN (" . implode( ",", $my_plugin_exclusions ) . ") )";
    
        return $exclusions;
    }
    function admin_stuff() {
        // some default values and instantiating some variables we'll be using
        $current_user = wp_get_current_user();
        $included = array();
        $parentcat = false;
    
        // we need a global variable to call on later, this can be changed to a class variable if desired
        $my_plugin_exclusions = array();
        global $my_plugin_exclusions;
    
        // check permissions
        if ( strpos( $current_user->adi_title, 'Support' ) !== false ) {
            $parentcat = get_cat_ID('Support');
        } elseif ( strpos( $current_user->adi_title, 'Sales' ) !== false ) {
            $parentcat = get_cat_ID('Sales');
        }
    
        // if we didn't set a parent value, we expect this to be a user with full access
        if ( ! $parentcat ) {
            return true;
        }
    
        // parent category is always approved
        $included[] = $parentcat;
    
        // get all categories in one list, and all child categories of the parent i nanother
        $allcats  = get_categories('hide_empty=0');
        $children = get_categories('hide_empty=0&child_of=' . $parentcat);
    
        // loop over the child categories os we get na array list for easy searching
        foreach( $children as $child ) {
            $included[] = $child->cat_ID;
        }
    
        // loop over every category, we need a list of the ones the user does NOT have permission to view, these are put into the global variables
        foreach( $allcats AS $child ) {
            if ( ! in_array( $child->cat_ID, $included ) ) {
                $my_plugin_exclusions[] = $child->cat_ID;
            }
        }
    
        add_filter( 'list_terms_exclusions', 'exclude_args', 10, 2 );
        return true;
    }
    add_action( 'admin_init', 'admin_stuff' );

    Special thanks to Clorith at #wordpress.

Viewing 1 replies (of 1 total)
  • The topic ‘Only one category and subcategories in admin area’ is closed to new replies.