Hide certain post types from editors

I'm trying to hide a series of posts from my editors in the admin panel based on either custom post type or category (whichever is easiest).

I tried adding a function such as:

add_action( 'admin_menu', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
global $current_user;
get_currentuserinfo();
if($current_user-user_login = 'test2') {
    remove_submenu_page( 'edit.php?post_type=post', 'edit.php?post_format=imageamp;post_type=post' );
        }
}

(In this snippet, I'm hiding it from one specific account) but it doesn't seem to work. Any ideas on how to do this?

Topic add-editor-style admin-menu functions Wordpress

Category Web


Here a little function which takes care to hide (In admin) Custom Post Types.

function remove_from_admin_menu(){
    // Check capability (admin)
    if( current_user_can( 'edit_dashboard' )){
        //Do nothing
    } elseif ( current_user_can( 'moderate_comments' ) ) { // editor capability
        remove_menu_page( 'edit.php?post_type=cpt01' ); // add here your cpt name
        remove_menu_page( 'edit.php?post_type=cpt02' );             
        remove_menu_page( 'edit.php?post_type=cpt03' );
        // for submenu pages
        remove_submenu_page( 'edit.php?post_type=testimonials-widget', 'testimonialswidget_settings' ); 

    }
}
add_action('admin_menu', 'remove_from_admin_menu');

FYI:
About roles/capabilities see codex.
Remove menu page. (Codex)
Remove sub-menu page.(Codex)
Credits to michael-cannon for his sub-menu cpt sample.(Github)

To do the same kind of trick for categories you could take a look here because it is unclear to me if that is what you realy want to do that way. If yes, then the credits should go to him and not to me :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.