I am using wordpress latest version and running a multi author blog. When any user (contributor or author) go to edit.php, they are seeing posts and post counts of other users. I hide the posts of other authors by this code,

function posts_for_current_author($query) {
    global $user_level;

if($query->is_admin && $user_level < 5) {
    global $user_ID;
    $query->set('author',  $user_ID);
    unset($user_ID);
}
unset($user_level);

return $query;

}
add_filter('pre_get_posts', 'posts_for_current_author');

Now they are not seeing other posts but seeing total post counts and published posts count. See screenshot:

editor.php screen of contributors

I wan’t to hide all and published section from users edit.php and other section will count only own posts. Please help me to do this!

1 Answer
1

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

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *