How can I add an Author filter to the Media Library?

In your question you showed the image which is grid-view and that codex belongs to list-view. So I am confused here for which section you are talking about.

Grid View : not possible unless you change the core as they don’t provide any hook and the whole media section was created by jquery, backbone and unserscore. Please look into media-grid.min.js or media-grid.js for code.

List View : in this view you can easily add dropdown. here is the script to do this. If you want then add parse_query or pre_get_posts filter to change the query for the dropdown. As I am setting author to url, wordpress itself sets it for me so need for those filters.

function media_add_author_dropdown()
{
    $scr = get_current_screen();
    if ( $scr->base !== 'upload' ) return;

    $author   = filter_input(INPUT_GET, 'author', FILTER_SANITIZE_STRING );
    $selected = (int)$author > 0 ? $author : '-1';
    $args = array(
        'show_option_none'   => 'All Authors',
        'name'               => 'author',
        'selected'           => $selected
    );
    wp_dropdown_users( $args );
}
add_action('restrict_manage_posts', 'media_add_author_dropdown');

Here are the references

  1. restrict_manage_posts
  2. pre_get_posts
  3. parse_query

Edit #1: Added author filter after this comment

function author_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
        if (isset($_GET['author']) && $_GET['author'] == -1) {
            $query->set('author', '');
        }
    }
}
add_action('pre_get_posts','author_filter');

Leave a Comment