How to hide the tags “all, publish, thrash, draft, pending” for authors posts but not for the administrator?

I had find the code below and it is hide the tags “all, publish, thrash, draft, pending” for authors posts BUT is also keep hiding the tags for administrator too !

function remove_edit_post_views( $views ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        unset($views['all']);       
        unset($views['publish']);       
        unset($views['trash']);
        unset($views['draft']);       
        unset($views['pending']);
        return $views;
    }
}    

add_action( 'views_edit-post', 'remove_edit_post_views' );

How can be showing those tabs for administrator?

Any help is appreciated!

Sorry for my not good english

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to return the $views for admins, thats why you don’t see anyting.

EDIT: IMPORTANT, you are using a action but you need to use filter

function remove_edit_post_views ($views) {
    if (!current_user_can('manage_options')) {
        unset($views['all']);
        unset($views['publish']);
        unset($views['trash']);
        unset($views['draft']);
        unset($views['pending']);

        return $views;
    }

    return $views; // this is the missing piece
}

add_filter('views_edit-post', 'remove_edit_post_views');

You can make it even shorter by doing this

function remove_edit_post_views ($views) {
    if (!current_user_can('manage_options')) return [];
    return $views;
}

add_filter('views_edit-post', 'remove_edit_post_views');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x