wordpress remove post status count from cms

I want to remove the post status count from WordPress edit.php.

enter image description here

Read More

My WordPress CMS have more than 500,000 posts. The publish count is loaded every time you open the page. The following query is fired every time.
This makes my WordPress CMS loading very slow.

SELECT post_status, COUNT( * ) AS num_posts FROM wp_posts WHERE post_type = 'post' GROUP BY post_status

Related posts

2 comments

  1. By tracing the code, I’ve worked up the only solution that I can see.

    The filter bulk_post_updated_messages is ONLY called on the edit.php screen.

    The counts are not calculated (in class-wp-posts-list-table.php, get_views method) if the global variable $locked_post_status is not empty.

    By gluing these two pieces of information together, I’ve got a solution that you can use by dropping it into your theme’s functions.php file:

    // Hook this filter, only called on the `edit.php` screen.
    // It's not the "correct" filter, but it's the only one we can leverage
    // so we're hijacking it a bit.
    add_filter('bulk_post_updated_messages', 'suppress_counts', 10, 2);
    
    // We need to let the function "pass through" the intended filter content, so accept the variable $bulk_messages
    function suppress_counts($bulk_messages) {
        // If the GET "post_type" is not set, then it's the "posts" type
        $post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
    
        // List any post types you would like to KEEP counts for in this array
        $exclude_post_types = array('page');
        // Global in the variable so we can modify it
        global $locked_post_status;
    
        // If the post type is not in the "Exclude" list, then set the $locked variable
        if ( ! in_array($post_type, $exclude_post_types)) {
            $locked_post_status = TRUE;
        }
    
        // Don't forget to return this so the filtered content still displays!
        return $bulk_messages;
    }
    
  2. i came up with this solution.

    //Disable Article Counter - query runs for about 1-2 seconds
            add_filter('admin_init', function () {
                foreach (get_post_types() as $type) {
                    $cache_key = _count_posts_cache_key($type, "readable");
                    $counts = array_fill_keys(get_post_stati(), 1);
                    wp_cache_set($cache_key, (object)$counts, 'counts');
                }
            }, -1);
            add_action('admin_head', function () {
                 $css  = '<style>';
                 $css .= '.subsubsub a .count { display: none; }';
                 $css .= '</style>';
    
                 echo $css;
            });
    

    the post counter uses the wp-cache, the idea behind this approach is, to prefill the cache with the “correct” object containing 1’s (0 would skip the status from being clickable) – at the earliest moment.

    it results in all stati being displayed – with 1’s and the query is not run et-all
    in addition it returns a css snippet to hide the (1)

    enter image description here

Comments are closed.