How to allow editors to access only specific posts in WordPress

16 Aug

How to allow editors to access only specific posts in WordPress

Context: Wondering, How to allow editors to access only specific posts in WordPress ? Yes, it can be done.

Solution: We know that by default WordPress has 6 user roles. And we can also create a lot of custom user roles. But sometimes we may need quite different ability. For example i want editor users to be able to edit only specific pages, not all pages. Contributor user role is not solution (contributor can edit only his own created posts, but in our case it doesn’t matter who has created posts,pages. And more than one user should be able to edit specific post). Use this code in functions.php file and you are done.

if (is_user_logged_in()) {
 
  global $wpdb;
  $curuser=wp_get_current_user();
  $role = $wpdb->prefix . 'capabilities';
  $current_user->role = array_keys($curuser->$role);
  $role = $curuser->role[0];
  if ($role=='editor' and !empty($_GET["post"]) and $_GET["post"]!='123') { //change 666 to your specific post ID
      die ('No permission');
    }
 
}

How to allow editors to access only specific posts in WordPress ? Is a general question that often people like to ask. If we use the above code snippet in functions.php file then editor will only be able to edit post with id 123. This way, we can only allow editor to edit specific posts or page.

Bydr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.