WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Filter Reference/ajax query attachments args

Description

The ajax_query_attachments_args filter is used to filter the query that fetches the attachments displayed in the media library modal on the post edit screen.

The filter is used like this

<?php add_filter( 'ajax_query_attachments_args', 'filter_function_name', 10, 1 ) ?>

Where filter_function_name is the function WordPress should call when the query is being modified. Note that the filter function must return the query array after it is finished processing, or the query will be empty and no attachments will be shown.

filter_function_name should be unique function name. It cannot match any other function name already declared.

Examples

Only Show Current User's Attachments

add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 );

function show_current_user_attachments( $query = array() ) {
    $user_id = get_current_user_id();
    if( $user_id ) {
        $query['author'] = $user_id;
    }
    return $query;
}

Note that $query is an array - this means that you can modify (or remove) existing arguments as well as add new ones.

See Also