I'm using this code in functions.php to hide/remove some pages from custom users backend and works well, but how can I reach to hide the subpages as well?
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' && current_user_can( 'custom_role' )) {
$query->query_vars['post__not_in'] = array('1','2','3');
}
}
I'm using this code in functions.php to hide/remove some pages from custom users backend and works well, but how can I reach to hide the subpages as well?
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' && current_user_can( 'custom_role' )) {
$query->query_vars['post__not_in'] = array('1','2','3');
}
}
Share
Improve this question
asked Jan 25, 2017 at 11:16
mesumesu
113 bronze badges
2 Answers
Reset to default 1One way to achieve this is by looping through the parent pages and fetching their respective children pages ids.
The resulting arrays can then be merged and used in the 'post__not_in'
variable.
add_filter( 'parse_query' , 'exclude_pages_from_admin' );
function exclude_pages_from_admin( $query ) {
global $pagenow,$post_type;
$pages = array('1','2','3');
foreach ( $pages as $parent_page ) {
$args = array(
'post_type' => 'page',
'post_parent' => $parent_page,
'fields' => 'ids',
);
$children = new WP_Query( $args );
$pages = array_merge( $pages, $children );
}
if ( is_admin() && $pagenow == 'edit.php' && $post_type == 'page' && current_user_can('custom_role') ) {
$query->query_vars['post__not_in'] = $pages;
}
}
Edit: Sorry I think I got the wrong end of the stick on this question! This method is for removing menu page.
I think a simpler method might be:
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
global $user_ID;
if ( current_user_can( 'author' ) ) {
remove_menu_page( 'edit.php' ); // Posts
remove_menu_page('upload.php'); // Media
remove_menu_page('edit-comments.php'); // Comments
remove_menu_page('tools.php'); // Tools
remove_menu_page( 'wpcf7' ); // Contact Form 7
remove_menu_page('acf-options'); //acf options
}
if ( current_user_can( 'editor' ) ) {
remove_menu_page('upload.php'); // Media
remove_menu_page('edit-comments.php'); // Comments
remove_menu_page('tools.php'); // Tools
remove_menu_page( 'wpcf7' ); // Contact Form 7
remove_menu_page( 'edit.php?post_type=acf' ); // ACF
remove_menu_page( 'admin.php?page=cptui_manage_post_types' ); //CPT UI
}
}
This is straight out of a current project that I am working on and works a treat!