Hide some posts from the editors

Hide posts from editors

Hide posts from editors

WordPress user with the “Editor” role assigned is capable to edit any post and page. But what to do in such cases as this one?
– “For example I have a client with ‘editor’ capabilities, but I don’t want him to be able to edit the homepage and one or two other pages on the website. Mainly because they contain a lot of styling which is easily disrupted in the wrong hands.”
How to prevent your clients from ruin their site due to lack of expertise at WordPress and HTML/CSS coding?
That’s not so difficult. Get those pages and posts IDs. Copy and paste code snippet below into site active theme functions.php file.

function query_exclude_posts($query) {
 
	global $current_user;
 
	// do not limit user with Administrator role
	if (current_user_can('administrator')) {
		return;
	}
 
	// put posts and pages IDs to exclude into this array
	$posts_to_exclude = array(387, 2771, 2325);
 
	$suppressing_filters = $query->get('suppress_filters'); // Filter suppression on?
 
	if (!$suppressing_filters && is_admin() && current_user_can('edit_posts') && current_user_can('edit_others_posts')) {
		$query->set('post__not_in', $posts_to_exclude);
 
	}
 
}
add_action('pre_get_posts', 'query_exclude_posts');

Replace sample posts and pages IDs there to your own, which you wish to hide from your site power users.

That’s it. Good Luck!

Tags: