1

Recently, I am customizing the COLORMag theme in WordPress. I want to make my site as a multi-author website where random people will register and write. But as my theme's homepage shows all the posts by categories, so only I want to control which categories post to show on the homepage. To do this, I have to restrict some categories from the meta box so that only I can select which post to show in that category. I have created two custom user roles by Publishpress capabilities plugin.

Two of them are "Authors under review" and "Publisher". The authors under review cannot post directly to the homepage, but publishers can do. Now, I want to restrict some specific categories for both of them to select from the meta box while posting content. But the categories will show in my editor pannel as a super admin and the posts of that category will be visible to all. I hope I have been able to explain the problem. The problem is so urgent to be solved. And if you can give me the custom code with explanation and instruction then it will be highly appreciated as I am so new in coding. Thanks in advance.

N.B: There was a plugin in the Wordpress directories to restrict categories. But that plugin has been removed from the Wordpress due to security reason.

  • I needed the same thing and used en-au.wordpress.org/plugins/restrict-categories Although it hasn't been updated for a while it does seem to work with Wordpress 5.3.2 but only if you enable classic editor rather than use Block Editor. It might get you started and point you in the right direction. Normally I wouldn't encourage using old plugins, but it solved the problem for me. – Admiral Noisy Bottom Feb 3 at 23:52
  • Sorry to say, now it is not possible to use the classic editor. Already, I have created some posts with the new editor. If I use classic editor now, it can show invalid JSON response. I need to change the meta box problem in the new editor. Is it possible? Can you please help? And interestingly, I have already tried your suggested plugin with the new editor but it didn’t work. Thanks for your suggestion though. ❤ – Naeem Musa Feb 4 at 0:00
  • Would it not be easier to use a custom taxonomy and hide it's box for those user roles? Then you could even have custom archives if you wanted to with their own URLs and templates! Or, set it to private and only use it in code and WP Admin – Tom J Nowell Feb 4 at 0:18
  • @NaeemMusa I was unaware of the invalid json issue. All my posts were created in the block editor, and I now use the classic editor and am able to edit and display articles without issue. I'm afraid I wont be much help here and I suggest looking at Tom's comment. – Admiral Noisy Bottom Feb 4 at 0:29
3

After searching a lot over the web I found a solution, so I am answering my own question. I hope it might help others. you can pass in the ids of categories you want to hide as an array, and then paste this in your functions.php file. It will work in the new editor. All categories will be shown for administrators.

Don't forget to customize the values in the $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 );
| improve this answer | |
  • If you have a lot of terms to be excluded could you instead use a NOT clause in your SQL to change the list from a exclusionary list to an inclusionary one? – ckoerner Jul 24 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.