I am working on a multi-user WordPress setup and have made it so a particular type of user can only see and interact with posts, images, pages etc that they have authored. The code to make this happen looks like this:
add_filter('pre_get_posts', 'current_author_posts');
function current_author_posts($query) {
if ($query->is_admin && current_user_can('artist')) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
This works well within the admin interface but leaves all of the post counts displaying incorrectly:
Are you aware of any filters or hooks to manipulate these figures and be correct across posts, pages, media and custom post types?
Many thanks.
I got this almost working, but refinements are needed to fit the specifics of the question and to deal with Attachments and Post-Types differently (see comments in code)…
First, I think it’s worth noting how I found the filter:
apply_filters( 'views_' . $screen->id, $views )
inspect element
do a global search in
/wp-admin
and/wp-includes
for subsubsub (funny class name, btw)…/wp-admin/includes/class-wp-list-table.php
As of writing, this functionality now resides in the class WP_List_Table, in the method “views()”.
The filter now looks like this:
$views will contain an array of each list element:
You can hook it up at the current_screen hook with a priority >10:
You can then add/change/remove upon elements in the list.
I know this is an old post, but I have run across it several times looking for a better solution, so I figured I’d add a little something to the mix. Until today, I have been parsing out each individual number from the strings filtered by
view_{$screen->id}
. But, I just learned these numbers are parsed in at line 248 of class-wp-posts-list-table.php. Thisview_{$screen->id}
filter is supposed to be used to add and remove views, not change the numbers (stupid me).The function that does all the calculating of the numbers in the parenthesis of the view string is
wp_count_posts()
. And, its got a filter with the same name that’s been around since version 3.7.0 (released on October 24, 2013—two years after this question was asked).There are 2 options. Let’s assume that your custom post type key is “arts-and-crafts-movement”
OPTION 1
Using the hook
views_{$this->screen->id}
here:https://developer.wordpress.org/reference/hooks/views_this-screen-id/
You should hook like this :
Then you should add somewhere a function called
filter_list_table_views
to filter the views:OPTION 2
Using the hook
wp_count_posts
here:https://developer.wordpress.org/reference/functions/wp_count_posts/