5

I need to hide/remove the numbers behind the Edit screen in the backend.

All (30) | Published (22) | Draft (5) | Pending (2) | Trash (1)

As I am running a multi author blog and each author has just access to its own posts, I dont want to publish the cumulated information of all authors.

With the following code the views are completely unset, but I dont want to remove the whole functionality:

function remove__views( $views ) {
unset($views['all']);
unset($views['publish']);
unset($views['trash']);
return $views;
}
add_action( 'views_edit-post',  'remove_views' );
add_action( 'views_edit-movie', 'remove_views' );

Has anybody an idea, how I can either hide/remove the numbers behind the edit screen or - at best - to show only the numbers related to each author?

1
  • Note that there's a typo in your code - remove__views has two underscores, but should only have one.
    – rinogo
    Commented Jun 23, 2016 at 1:22

4 Answers 4

7

There is, unfortunately, no "pretty" way to do this (i.e. without using string replacing or rewriting a big chunck of functionality). So, resorting to preg_replace...

We'll need to filter the links, and it's good to see that you've already found the proper filters! Looping through the views and using a regular expression, we can remove the element containing the post count. Implementing this for posts, you'll need something like this:

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

function wpse149143_edit_posts_views( $views ) {
    foreach ( $views as $index => $view ) {
        $views[ $index ] = preg_replace( '/ <span class="count">\([0-9]+\)<\/span>/', '', $view );
    }

    return $views;
}
1
  • Thanks for the code, its working great. If there is a solution to show only the numbers related to each author - instead of hiding all counts - please let me know.
    – Traveler
    Commented Jun 10, 2014 at 12:52
6

We had the same problem today. A pretty solution is perhaps not obvious, but not impossible either.

Here is the code we use to show only the counts related to each author. Users with rights to edit the posts of other users will still see the full count.

add_filter('wp_count_posts', 'wpse149143_wp_count_posts', 10, 3);


/**
 * Modify returned post counts by status for the current post type.
 *  Only retrieve counts of own items for users without rights to 'edit_others_posts'
 *
 * @since   26 June 2014
 * @version 26 June 2014
 * @author  W. van Dam
 *
 * @notes   Based on wp_count_posts (wp-includes/posts.php)
 *
 * @param object $counts An object containing the current post_type's post
 *                       counts by status.
 * @param string $type   Post type.
 * @param string $perm   The permission to determine if the posts are 'readable'
 *                       by the current user.
 * 
 * @return object Number of posts for each status
 */
function wpse149143_wp_count_posts( $counts, $type, $perm ) {
    global $wpdb;

    // We only want to modify the counts shown in admin and depending on $perm being 'readable' 
    if ( ! is_admin() || 'readable' !== $perm ) {
        return $counts;
    }

    // Only modify the counts if the user is not allowed to edit the posts of others
    $post_type_object = get_post_type_object($type);
    if (current_user_can( $post_type_object->cap->edit_others_posts ) ) {
        return $counts;
    }

    $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND (post_author = %d) GROUP BY post_status";
    $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type, get_current_user_id() ), ARRAY_A );
    $counts = array_fill_keys( get_post_stati(), 0 );

    foreach ( $results as $row ) {
        $counts[ $row['post_status'] ] = $row['num_posts'];
    }

    return (object) $counts;
}
0

I think the easiest way would be to just hide them with CSS, you could try something like this:

add_action( 'admin_head', 'wpse145565_hide_post_count' );

function wpse145565_hide_post_count() {
    $css  = '<style>';
    $css .= '.subsubsub a .count { display: none; }';
    $css .= '</style>';

    echo $css;
}

Basically the function adds a style tag to the head of the admin section that sets the display property of the text count as none.

0

In WordPress 5.7, the above code is not working properly. For example, in the case of 'mine', it does not work. So I modified the above code.

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

    global $wpdb;

    foreach ($views as $index => $view){
        
        if (in_array($index, array('all')))
            continue;
        
        // There is no mine for post_status. However, it can be derived using the summation of pending and publish count.
        if(strcmp('mine',$index)== 0){
            
            $viewValue = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type='post' AND post_author = $iCurrentUserID and (post_status='pending' || post_status='publish' || post_status='draft')");
            
        }else{
            
            $viewValue = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type='post' AND post_author = '" . $iCurrentUserID . "' AND post_status='$index'");
            
        }
        
        // Replace the existing value from the update one.
        $views[$index] = preg_replace('/\(.+\)/U', '(' . $viewValue . ')', $views[$index]);
        
    }


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

}

# It is for non admin users. For example contributors, etc.
if (!current_user_can('manage_options')){
    add_filter("views_edit-post", 'custom_editor_counts', 10, 1);
}

For any help, anyone can contact me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.