Block posting to selected categories

Exclude category for role

Exclude category for role

Block posting to the list of selected categories on per role base – is it possible? If you wish to limit your WordPress blog authors or users with custom created role from posting to some categories only, you can do it with piece of PHP code below (read entire post to get it.).

Just copy it and paste it into your active theme functions.php file (wp-content/themes/your-theme/functions.php) insert ID of categories to block. You can get ID of category at Categories page. Move mouse over ‘Edit’ link of selected category and look on the link at the bottom of your browser. Search ‘tag_ID=’ there. The number to the right of it is the category ID.
Congratulations! That’s all. You got it.

if (current_user_can('author')) {  // change 'author' role to your own if needed
  if (is_admin()) {
    function limit_categories_for_role($exclusions) {
      $cats_to_exclude = array(14, 30);  // insert you categories ID to exclude for entire role here
      foreach ($cats_to_exclude as $cat_id) {
        $exclusions .= " AND (t.term_id<>$cat_id)";
      return $exclusions;
    add_filter('list_terms_exclusions', 'limit_categories_for_role');

In case you need to block for role ability to post into all categories except one, two or some small subset, and your categories list is large or extended too often, then other code will be more effective. We get full categories list for begin here, then we delete from it those categories, to which we plan to give access.

 if (current_user_can('author')) {	// change 'author' role to your own if needed
	if (is_admin()) {
		function limit_categories_for_role($exclusions) {
			remove_filter('list_terms_exclusions', 'limit_categories_for_role');  // delete our filter in order to avoid recursion when we call get_all_category_ids() function
			$cats_to_exclude = get_all_category_ids();	// take full categories list from WordPress
			add_filter('list_terms_exclusions', 'limit_categories_for_role');  // restore our filter
			$cats_to_save = array(21, 25); // insert here categories ID, to which you wish to give access for this role
			$cats_to_exclude = array_diff($cats_to_exclude, $cats_to_save); // delete ID, to which we give access, from the full categories list
			$cats_to_exclude = '('. implode( ',', $cats_to_exclude) .')'; // build WHERE expression for SQL-select command
			$exclusions = " AND (t.term_id not IN $cats_to_exclude)";
			return $exclusions;
		add_filter('list_terms_exclusions', 'limit_categories_for_role');

Tags: , ,

  • Miguel Alvarez

    Is posible to made that with custom post ?

  • As all categories are stored in the same database table filtering them by ID should work for all post types, as default, as custom.

  • Dudefromtexas

    this hack seems to work great for posts with categorys.

    for this site I never made categories (only have the default uncategorized, oops) but I have several diff post types.

    I am trying to limit the users ability to see specific post types. 

    example I have a post type called lists and a post type called clients. I want them to be able to see their lists and edit them but not the clients. I want the user to edit some post types but not others. since Im not the smartest crayon in the box I cant seem to figure out how to change the script above to work for post types. 

    feedback or assistance would be appreciated.  btw, the user role editor plugin rocks!

  • Thanks. 
    Custom post type could have its own custom user capability defined. If such capability is not defined, then default (‘posts’) is used, ‘edit_posts’, ‘publish_posts’ for example. If it’s defined, then it could be ‘edit_lists’, ‘publish_lists’ already.This way you may isolate different post types between different user roles by including/excluding correspondent capabilities. Check plugin which you use to define custom post type, you will see special field, e.g. ‘capability’ or ‘permission’ there.

  • gus

    it could work with pages? i mean i need that the user only can publish in one page

  • It could be easier to look on these plugins:
    Plugins authors declare content restriction functionality on per role basis.

  • Cristian

    Vladimir, how do you replace the hardcoded IDs from

    $cats_to_save = array(21, 25);

    with child categories of a certain category?

  • serenasme

    Hi Vladimir, I need to block for role ability to post into all categories except one, so the second code is perfect for me. And it works, that role now can see only one category (example id=5). BUT if users don’t check the category id=5, just because they forget to check, then the system will assign category id=1: a forbidden category.