1

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');
 }

}
1

One 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;
    }

}
0

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!

1
  • Err, the OP doesn't want to remove menu but rather remove pages and sub pages from the Page list table on the backend
    – Tunji
    Jan 25 '17 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.