apply_filters( 'ajax_query_attachments_args', array $query )

Filters the arguments passed to WP_Query during an Ajax call for querying attachments.


Description Description

See also See also


Top ↑

Parameters Parameters

$query

(array) An array of query variables.


Top ↑

More Information More Information

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


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 a unique function name. It cannot match any other function name already declared.


Top ↑

Source Source

File: wp-admin/includes/ajax-actions.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
3.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Collins Mbaka

    Only Show Current User’s Attachments

    add_filter( 'ajax_query_attachments_args', 'wpdocs_show_current_user_attachments' );
    
    function wpdocs_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.

  2. Skip to note 2 content
    Contributed by Akira Tachibana

    (From Codex)
    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.

You must log in before being able to contribute a note or feedback.