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

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

Leave a Comment