Display only author posts in dashboard all posts panel
-
18-04-2021 - |
Question
I would like to display only authors own posts in dashboard all posts section. As of now it displays everything.
I found some code here which is written by @t31os Its working correctly.
function posts_for_current_author($query) {
global $user_level;
if($query->is_admin && $user_level < 5) {
global $user_ID;
$query->set('author', $user_ID);
unset($user_ID);
}
unset($user_level);
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
But user_level is deprecated. So can anyone modify the code for new version? Thanks
La solution
The following worked for me to show only the current user's posts in the admin
add_action( 'load-edit.php', 'posts_for_current_author' );
function posts_for_current_author() {
global $user_ID;
/*if current user is an 'administrator' do nothing*/
//if ( current_user_can( 'add_users' ) ) return;
/*if current user is an 'administrator' or 'editor' do nothing*/
if ( current_user_can( 'edit_others_pages' ) ) return;
if ( ! isset( $_GET['author'] ) ) {
wp_redirect( add_query_arg( 'author', $user_ID ) );
exit;
}
}
It can be edited pretty easily if the restriction should only happen for users in a particular role.
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange