Authors see only their own posts and media
In the admin area, users who can’t edit other people’s posts (authors, contributors) only see their own posts and their own uploads in the lists and the media library.
<?php
add_action(
'pre_get_posts',
static function ( $query ) {
if ( ! is_admin() || ! $query->is_main_query() || current_user_can( 'edit_others_posts' ) ) {
return;
}
global $pagenow;
if ( in_array( $pagenow, array( 'edit.php', 'upload.php' ), true ) ) {
$query->set( 'author', get_current_user_id() );
}
}
);
// The media library's grid view and the media picker in the editor.
add_filter(
'ajax_query_attachments_args',
static function ( $args ) {
if ( ! current_user_can( 'edit_others_posts' ) ) {
$args['author'] = get_current_user_id();
}
return $args;
}
);
Good to know
The counts above the list (All, Published…) still include everyone’s posts.
In these packs
- More admin tweaks: Useful columns, a tidier admin bar, a welcome box for clients, and fewer menus for editors.
- Users and profiles: Registration dates, last logins, social profile fields, longer sessions and stricter passwords.