Is it possible to Remove those menu items for a Post and a Custom Post Type page ? thank you

enter image description here

add_filter( 'views_edit-post', function( $views )
    if( current_user_can( 'manage_options' ) )
        return $views;

    $remove_views = [ 'all','publish','future','sticky','draft','pending','trash' ];

foreach( (array) $remove_views as $view )
    if( isset( $views[$view] ) )
        unset( $views[$view] );
return $views;
} );

 * Force the 'mine' view on the 'edit-post' screen
add_action( 'pre_get_posts', function( \WP_Query $q )
        && $q->is_main_query() 
        && 'edit-post' === get_current_screen()->id 
        && ! current_user_can( 'manage_options' )
        $q->set( 'author', get_current_user_id() ); 
} );'

This code comes from this answer.

This is working great for posts. But I can't wrap my head around using it for a custom post type.

  • Thank you Charles, I tried to do my due diligence. I have working code for the "posts page" However I can't code it correct to work with a custom post type. Replace -> 'views_edit-post' with a custom post type. – Peter B Jun 2 '17 at 15:45
  • @Charles after he added some code, I think his question is how to implement views_edit-{$post_type}, as Peter says, he's managed it for posts. – hwl Jun 2 '17 at 15:59

responding to code added by OP

replace post with the slug of your post type.

add_filter('views_edit-SLUG_OF_YOUR_POST_TYPE', function($views) { ...

So a custom post type of Library Books with a slug of library-books:

add_filter('views_edit-library-books', function($views) {...

original response

Use $wp_roles->remove_cap( $role, $cap ); on all roles except the admin, removing the edit_others_posts cap. See this for caps and roles.

  • Thanks for the fast reply. I have it working now for "posts". with the code a pasted above. I can't wrap my head around a custom post type to replace 'views_edit-post' – Peter B Jun 2 '17 at 15:31
  • i've updated the answer with an example – hwl Jun 2 '17 at 15:43
  • @PeterB please mark the answer accepted to close the question thanks. You need to register for that. – birgire Jun 2 '17 at 15:59

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.