How to hide posts count and posts of other users from edit.php for contributors and authors

In order to remove a specific All() and Pubished() post counts from Posts-editor page you can:

// Create a specific hook
add_filter("views_edit-post", 'custom_editor_counts', 10, 1);


function custom_editor_counts($views) {
    // var_dump($views) to check other array elements that you can hide.
    unset($views['all']);
    unset($views['publish']);
    return $views;
}

Lets go more advanced and remove All() from Posts-editor admin table and Published() from Pages-editor admin table.

// Create a hook per specific Admin Editor Table-view inside loop
foreach( array( 'edit-post', 'edit-page') as $hook )
    add_filter( "views_$hook" , 'custom_editor_counts', 10, 1);


function custom_editor_counts($views) {

    // Get current admin page view from global variable 
    global $current_screen;

    // Remove count-tab per specific screen viewed.
    switch ($current_screen->id) {
        case 'edit-post':
            unset($views['all']);
            break;
        case 'edit-page':
            unset($views['publish']);
            break;
    }

    return $views;
}

Refferences:
[Hide the post count behind Post Views (Remove All, Published and Trashed) in Custom Post Type]

UPDATE

Updated an answer with a code how to update posts count for specific user, excluding “all” and “publish” views. Recently tested on WP 4.3 twentyfifteen.

add_filter("views_edit-post", 'custom_editor_counts', 10, 1);

function custom_editor_counts($views) {
    $iCurrentUserID = get_current_user_id();

    if ($iCurrentUserID !== 0) {

        global $wpdb;

        foreach ($views as $index => $view) {
            if (in_array($index, array('all', 'publish')))
                continue;
            $viewValue = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type="post" AND post_author=$iCurrentUserID AND post_status="$index"");
            $views[$index] = preg_replace('/\(.+\)/U', '(' . $viewValue . ')', $views[$index]);
        }

        unset($views['all']);
        unset($views['publish']);
    }
    return $views;
}

Leave a Comment