How can I hide a category from Contributors in the edit/add new post screen?

I have a situation where I need to hide a specific category and it’s children from users who are logged in as Contributors. I don’t want them to see this category and it’s children in the categories meta-box on the add new post screen.

I can’t find a plugin (that works) to do this, wondering if someone else knows of one, or even better if there is a function I can use to do this?

Related posts

Leave a Reply

5 comments

  1. Working Example in Oct 2020, Working answer, I went to different answers none of them works now in new WordPress.

    function hide_categories_for_specific_user( $exclusions, $args ){
    
    if ( ((defined( 'REST_REQUEST' ) && REST_REQUEST) or $GLOBALS['pagenow'] === 'edit.php' ) && !current_user_can( 'manage_options' ) ) {
    
       // IDs of terms to be excluded
       $exclude_array = array("12","16","17"); // CHANGE THIS TO IDs OF YOUR TERMS
    
    
       // Generation of exclusion SQL code
       $exterms = wp_parse_id_list( $exclude_array );
       foreach ( $exterms as $exterm ) {
               if ( empty($exclusions) )
                   $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
           else
                   $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
       }
    
       // Closing bracket
       if ( !empty($exclusions) )
       $exclusions .= ')';
    
       // Return our SQL statement
    
      }
       return $exclusions;
    }
    
     // Finally hook up our filter
     add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );
    

    answer final help from Hide Some Categories in Post Editor

  2. Hi @davemac:

    Well, I wrote this before I saw that you answered your own question so I might as well post it anyway:

    add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
    function yoursite_list_terms_exclusions( $exclusions, $args ) {
      global $pagenow;
      if (in_array($pagenow,array('post.php','post-new.php')) && 
         !current_user_can('see_special_cats')) {
        $exclusions = " {$exclusions} AND t.slug NOT IN ('slug-one','slug-two')";
      }
      return $exclusions;
    }
    

    This code presumes that you’ve used a plugin like the Members plugin to create a capability called 'see_special_cats' and that you’ve assigned it to every role that you want to have access to the categories except of course 'Contributors'.

    Since you found the plugin you may not need this, but maybe it will help someone else.

  3. if you want hide category from contributor but only admin can see that category then change ‘see_special_cats' to 'manage_options' I’ve tested and it works hope it helps. Thanks! 🙂

  4. After searching a lot over the web I found a solution,
    you can pass in the ids of categories you want to hide as an array, and then paste this in your functions.php file. All categories will be shown for administrators

    $exclude_array = array(“5″,”6″,”7″,”1”);

    function hide_categories_for_specific_user( $exclusions, $args ){
    
    if (!current_user_can('manage_options') ) {
    
       // IDs of terms to be excluded
       $exclude_array = array("5","6","7","1"); // CHANGE THIS TO IDs OF YOUR TERMS
    
    
       // Generation of exclusion SQL code
       $exterms = wp_parse_id_list( $exclude_array );
       foreach ( $exterms as $exterm ) {
               if ( empty($exclusions) )
                       $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
               else
                       $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
       }
    
       // Closing bracket
       if ( !empty($exclusions) )
           $exclusions .= ')';
    
       // Return our SQL statement
       return $exclusions;
    
      }
    }
    
     // Finally hook up our filter
     add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );
    
  5. JavaScript solution – This is reliably future proof

    /**
     * Hide categories for specific user
     *
     */
    function hide_categories_for_csl_admin() {
        if (!current_user_can('a-capability')) {
            return;
        }
        ?>
        <script>
            (function() {
                document.addEventListener('DOMContentLoaded', function() {
                    var categories_to_hide = [1,167]; //define all the category ids you want to hide
                    categories_to_hide.forEach(function(category) {
                        var checkbox = document.querySelector('#category-' + category);
                        checkbox.style.display = 'none';
                    });
                });
            })();
        </script>
        <?php
    }
    add_action('admin_print_scripts-post.php', 'hide_categories_for_csl_admin');