I intended to run a multi-author site, I don’t want the posts from other authors to be shown in /wp-admin/edit.php
page.
I managed solve this problem by the codes from this thread. The code is like this:
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'manage_options' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
The codes work great, it hide the posts from other authors to be shown at here. But I do find another problem – the menu at top of the page doesn’t change the associated number of posts by the author, it shows the number of all the post in my site.
The menu I mean is like this:
Mine () | All () | Published () | Draft () | Trash ()
How to change the number in the ()
to reflect the number only associated to the author?
Here is what I use:
Source
Shorter solution based on answer https://wordpress.stackexchange.com/a/49200/83038.
NOTE: Available since WordPress 3.7.0.
The Best Way
ALL THESE ANSWERS HERE HAVE SECURITY CONCERNS.
The best way is adding custom capabilities and managing posts etc. by the capabilities.
An easy way
Artem’s solution seems to be better because WP doesn’t refer post counts only on post edit screen but also within Dashboard widget, Ajax response etc.
For better solution based on Artem’s one.
why:
wp_count_posts
earlierly returns the cached post counts when the result has been cached before.why: cache increases the performance.
$perm
parameter ofwp_count_posts
hook.why: the post counts should includes user’s own private posts based on
readable
perm.why: the filters might be overridden by other filters.
why: sticky posts count includes other’s posts and they are separatedly counted by
WP_Posts_List_Table
.why:
read_others_posts
capability could be modified.You might want to additional tweaks
post_author
query var toWP_Comment_Query
.wp_count_comments
hook.The following is a modified version based on
wp_post_counts()
of WP 4.8.Known Issue: Sticky posts that don’t belong to user are counted.fixed by removing sticky posts view.