4

Toward the top of the edit.php screens there is a list that displays post statuses along with the post count. I believe this is generated by WP_Post_List_Table::get_views. For example All (7) | Published (6) | Draft (1)

Unfortunately, those post counts do not respect filters. I am using pre_get_posts to exclude certain posts. Even though only four posts are visible to the user, the numbers still reflect the total post count. I would like to see All (4) | Published (3) | Draft (1)

I can't seem to find an action/filter to override these numbers (without using JavaScript).

2 Answers 2

7

You might want to remove those counts and replace them with your own.

function insert_post_counts($views){
    //Run your query to count posts
    //use wp_cache_set and wp_cache_get to optimize performance

    $edit_url = admin_url( 'edit.php' );

    $views['all'] = 'All <a href="'.$edit_url.'">('.$all_count.')</a>';
    $views['publish'] = 'Published <a href="'.$edit_url.'?post_status=publish">('.$publish_count.')</a>';
    $views['draft'] = 'Draft <a href="'.$edit_url.'?post_status=draft">('.$draft_count.')</a>';
    $views['trash'] = 'Trash <a href="'.$edit_url.'?post_status=trash">('.$draft_count.')</a>';

    return $views;
}
add_filter('views_edit-post, 'insert_post_counts', 10, 1);

You can reference how wp_count_posts works to generate the native post counts.

Hope this helps!

1
  • It's such a pain to have to write the query again.
    – Anh Tran
    Jun 20, 2021 at 0:56
1

Resurrecting an old thread...

There were a couple of minor code errors in the example above, as well as only the parenthetical counts being within the link tags. The following is something you can copy and paste directly in and use as a starting point:

function wp_insert_post_counts($views){
    //Run your query to count posts
    $all_count = 100;
    $publish_count = 200;
    $future_count = 250;
    $draft_count = 300;
    $trash_count = 400;

    //use wp_cache_set and wp_cache_get to optimize performance
    $view_slugs = array(array('all', NULL, 'All', $all_count), 
                        array('publish', 'publish', 'Published', $publish_count), 
                        array('future', 'future', 'Scheduled', $future_count), 
                        array('draft', 'draft', 'Draft', $draft_count), 
                        array('trash', 'trash', 'Trash', $trash_count) );
    $post_status_var = get_query_var('post_status');

    $edit_url = admin_url( 'edit.php' );

    $view_count = count($view_slugs);
    for ($x = 0; $x < $view_count; $x++) {
        $class = ($post_status_var == $view_slugs[$x][1]) ? ' class="current"' : '';
        $post_status_temp = $view_slugs[$x][1];
        if($post_status_temp != '') {
            $post_status_temp = '?post_status='.$view_slugs[$x][1];
        }
        $views[$view_slugs[$x][0]] = sprintf(__('<a href="' .
                               $edit_url .
                               $post_status_temp . '"' .
                               $class .
                               ' >' .
                               $view_slugs[$x][2] .
                               ' <span class="count">(%d)</span></a>'), 
                            $view_slugs[$x][3]);
    }

    return $views;
}
add_filter('views_edit-post', 'wp_insert_post_counts', 10, 1);

As suggested above, look at wp_count_posts to understand more about implementing your own post counts.

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.