Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Restrict editing pages by user role? › Reply To: Restrict editing pages by user role?
23/09/2015 at 03:28
#1711
Vladimir
Keymaster
AntKat,
This is a version of code modified to work with a list of pages slugs:
add_action('pre_get_posts', 'restrict_posts_list');
function restrict_posts_list($query) {
if ( !(is_blog_admin() && current_user_can('editor')) ) {
return $query;
}
$suppressing_filters = $query->get('suppress_filters'); // Filter suppression on?
if ($suppressing_filters) {
return query;
}
if ($query->query['post_type']=='page') {
$posts_restriction_type = 2; // Prohibit
$posts_slugs = array(
'jobs',
'post-to-our-blog',
'sample-page'
);
$posts_list = array(); // comma separated list of pages IDs
foreach($posts_slugs as $post_slug) {
$page = get_page_by_path($post_slug);
if ($page) {
$posts_list[] = $page->ID;
}
}
if (empty($posts_list)) {
return $query;
}
if ($posts_restriction_type==1) { // Allow
$query->set('post__in', $posts_list);
} else { // Prohibit
$query->set('post__not_in', $posts_list);
}
}
return $query;
}
// restrict_posts_list()