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,
$get_all_statuses;
$get_all_statuses = \'publish\', \'private\', \'draft\', \'status-assigned\';
$get_all_statuses = str_replace(" ","", $get_all_statuses); // clear any spacing
$get_all_statuses = explode(",", $get_all_statuses);
$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\', $get_all_statuses );
endif;
return $query;
}
add_filter(\'pre_get_posts\', \'posts_for_current_author\');
// Function to update the subsubsub links with new values
add_filter(\'views_edit-page\',\'wp37_update_page_quicklinks\',1);
function wp37_update_page_quicklinks($views) {
global $get_all_statuses; // status to show
$remove_statuses_out_array = array(\'pending\',\'scheduled\', status-open); //status to remove
//Get total count for ALL POST
$stat_count = wp_count_posts( \'page\' );
// Show ALL as first link
$views[\'all\'] = sprintf(__(\'<a href="%s" title="All Post">All <span class="count">(%d)</span></a>\', \'all\'), admin_url(\'edit.php?post_type=page\'), $stat_count);
foreach($get_all_statuses as $status_label => $status_name):
//echo "<br>".$status_name.": ".$stat_count->$status_name;
$views[$status_name] = sprintf(__(\'<a href="%s" title="\'.$status_label.\'">\'.$status_label.\'<span class="count">(%d)</span></a>\', $status_name), admin_url(\'edit.php?post_type=page&post_status=\'.$status_name), $stat_count->$status_name);
endforeach;
foreach( $remove_statuses_out_array as $rem ):
unset($views[$rem]);
endforeach;
return $views;
}
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
.