Get a count of post per status queried using pre_get_posts

My following query using pre_get_posts shows the Author only certain posts or pages with certain statuses that s/he should only see and of what belongs to him/her.

Is there any way I can get a count of each item shown based on status that renders in my WP List Table view (aka admin post table)?

I am trying to recreate my own version of the .subsubsub links and only show the numbers of the posts “in views” broken down by status, because the current WP default system does not count correctly when $query->set() is added in to change the default list views.

For example. If the query show that the current user has a total of 10 posts assigned, it should show like:

All (10) | Published (4) | Private (0) | Draft (0) | Assigned (6)

Even with my query working, it still shows ALL posts, all published, etc.

Here is my current code:

function posts_for_current_author($query) {

require_once(ABSPATH . ‘wp-admin/includes/screen.php’);
global $pagenow;

$current_user = wp_get_current_user();
$current_screen = get_current_screen();

if( ‘edit.php’ != $pagenow || ‘page’ != $current_screen->post_type || !$query->is_admin )
return $query;

if(current_user_can(‘author’)):

$query->set(‘author’, $current_user->ID );
$query->set( ‘post_status’, array( ‘publish’, ‘private’, ‘draft’, ‘status-assigned’) );

endif;

return $query;

}

add_filter(‘pre_get_posts’, ‘posts_for_current_author’);

I have tried anything I could think of, like:

$count_pages = wp_count_posts( $post_type = ‘page’, $perm = “” );

Or I’ve looked into this filter views_{$this->screen->id}:

$views = apply_filters( “views_edit-page”, $views );

But nothing seems to work. So I was wondering if there is a count query param somewhere. My last resort would be, I guess, to go into the wpdb.

Read more here:: Get a count of post per status queried using pre_get_posts

Leave a Reply

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