hide some categories

How to Hide Specific Categories in the Post Editor and Quick Edit

If you’re running a multi-author blog or have a team of website maintainers, you might want to restrict access to certain categories. For example, you may have an “Editorial” or “Selected Posts” category where only certain users (like administrators) should be able to assign posts. Hiding these categories from other editors or authors is a good way to ensure that only designated users can manage them.

While plugins like “Restrict Categories” or “Author Category” may have worked in the past, they no longer function as expected in newer versions of WordPress. However, you can still achieve this functionality by adding custom code to your theme’s functions.php file.

Important Note

Before proceeding, be aware that this method will hide the selected categories for all REST API requests. If your theme uses REST requests to display categories on the front end, this solution may not work for you.

Steps to Hide Categories

Add the Code to Your Theme

Paste the following code snippet into your theme’s functions.php file:

function hide_categories_for_specific_user( $exclusions, $args ){
if ( ((defined( 'REST_REQUEST' ) && REST_REQUEST) or $GLOBALS['pagenow'] === 'edit.php' ) && !current_user_can( 'manage_options' ) ) {
   $exclude_array = array("437","469"); //IDs OF YOUR TERMS
   $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) . ' ';
   }
   if ( !empty($exclusions) )
       $exclusions .= ')';
  }
   return $exclusions;
}
add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );

Replace Category IDs

In the code, replace 437 and 469 with the IDs of the categories you want to hide.

Find Category IDs

To find the ID of a category:

  1. Go to your WordPress dashboard and navigate to Posts > Categories.
  2. Hover over the category name.
  3. Look at the URL in your browser’s status bar. The category ID will be included as tag_ID=<number>.

For example:
https://example.com/wp-admin/term.php?taxonomy=category&tag_ID=437&post_type=post

In this case, the category ID is 437.

how to find category id

Once you’ve completed these steps, the specified categories will no longer appear in the post editor and quick edit options for users who are not administrators.

Limitations

  • If your theme uses the REST API to display categories on the front end, these categories will also be hidden there. In such cases, consider an alternative method or consult a developer.

This method is a simple and effective way to restrict certain categories from non-administrators on your WordPress site. If you need to hide more categories, simply add their IDs to the $exclude_array in the code.

If this solution worked for you, feel free to leave a comment below to share your experience!

1 thought on “How to Hide Specific Categories in the Post Editor and Quick Edit”

Leave a Comment

Your email address will not be published. Required fields are marked *