Wordpress - Restrict admin access to certain pages for certain users
This code seems to work well for me (in functions.php):
$user_id = get_current_user_id();
if ($user_id == 2) {
add_filter( 'parse_query', 'exclude_pages_from_admin' );
}
function exclude_pages_from_admin($query) {
global $pagenow,$post_type;
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__not_in'] = array('123','234','345');
}
}
It won't let me comment, so I'm adding this as a new answer.
The error:
Warning: "call_user_func_array() expects parameter 1 to be a valid callback, >function 'exclude_pages_from_admin' not found or invalid function name".
... is due to the function being called only after the user_id is checked. So if you're logged in NOT as that user, the function doesn't exist, and the filter returns that error, since it is looking for the function, but can't find it.
So it should be:
add_filter( 'parse_query', 'exclude_pages_from_admin' );
function exclude_pages_from_admin($query) {
$user_id = get_current_user_id();
if ($user_id == 2) {
global $pagenow,$post_type;
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__not_in'] = array('123','234','345');
}
}
}