wp_ajax_query_attachments

The timeline below displays how wordpress function wp_ajax_query_attachments has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Handles querying attachments via AJAX.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    update_post_parent_caches($attachments_query->posts);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? (int) ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 6.3

/**
 * Handles querying attachments via AJAX.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    update_post_parent_caches($attachments_query->posts);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 6.1

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    update_post_parent_caches($attachments_query->posts);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 9.5

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 5.9

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 8.6

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 8.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->get('posts_per_page');
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 8.1

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $posts_per_page = (int) $attachments_query->query['posts_per_page'];
    $max_pages = $posts_per_page ? ceil($total_posts / $posts_per_page) : 0;
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 5.8

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $attachments_query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $attachments_query->posts);
    $posts = array_filter($posts);
    $total_posts = $attachments_query->found_posts;
    if ($total_posts < 1) {
        // Out-of-bounds, run the query again without LIMIT for total count.
        unset($query['paged']);
        $count_query = new WP_Query();
        $count_query->query($query);
        $total_posts = $count_query->found_posts;
    }
    $max_pages = ceil($total_posts / (int) $attachments_query->query['posts_per_page']);
    header('X-WP-Total: ' . (int) $total_posts);
    header('X-WP-TotalPages: ' . (int) $max_pages);
    wp_send_json_success($posts);
}

WordPress Version: 7.8

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 7.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .10

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 6.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .10

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 5.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .11

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 4.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .12

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 3.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .14

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 2.3

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .20

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 2.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .17

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 1.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .15

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 0.3

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .20

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 0.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .18

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 9.6

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 9.3

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .22

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('wp_allow_query_attachment_by_filename', '__return_true');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .20

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 9.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: .10

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'author', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 4.7

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    // Filter query clauses to include filenames.
    if (isset($query['s'])) {
        add_filter('posts_clauses', '_filter_query_attachment_filenames');
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 4.6

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    /**
     * Filters the arguments passed to WP_Query during an Ajax
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 4.2

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $keys = array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum');
    foreach (get_taxonomies_for_attachments('objects') as $t) {
        if ($t->query_var && isset($query[$t->query_var])) {
            $keys[] = $t->query_var;
        }
    }
    $query = array_intersect_key($query, array_flip($keys));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    /**
     * Filter the arguments passed to WP_Query during an AJAX
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 4.1

/**
 * Ajax handler for querying attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $query = array_intersect_key($query, array_flip(array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum')));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    /**
     * Filter the arguments passed to WP_Query during an AJAX
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 4.0

/**
 * Ajax handler for querying for attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $query = array_intersect_key($query, array_flip(array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum')));
    $query['post_type'] = 'attachment';
    if (MEDIA_TRASH && !empty($_REQUEST['query']['post_status']) && 'trash' === $_REQUEST['query']['post_status']) {
        $query['post_status'] = 'trash';
    } else {
        $query['post_status'] = 'inherit';
    }
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    /**
     * Filter the arguments passed to WP_Query during an AJAX
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 3.9

/**
 * Query for attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $query = array_intersect_key($query, array_flip(array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in')));
    $query['post_type'] = 'attachment';
    $query['post_status'] = 'inherit';
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    /**
     * Filter the arguments passed to WP_Query during an AJAX
     * call for querying attachments.
     *
     * @since 3.7.0
     *
     * @see WP_Query::parse_query()
     *
     * @param array $query An array of query variables.
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}

WordPress Version: 3.7

/**
 * Query for attachments.
 *
 * @since 3.5.0
 */
function wp_ajax_query_attachments()
{
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array();
    $query = array_intersect_key($query, array_flip(array('s', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in')));
    $query['post_type'] = 'attachment';
    $query['post_status'] = 'inherit';
    if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) {
        $query['post_status'] .= ',private';
    }
    /**
     * Filter the arguments passed to WP_Query during an AJAX call for querying attachments.
     *
     * @since 3.7.0
     *
     * @param array $query An array of query variables. @see WP_Query::parse_query()
     */
    $query = apply_filters('ajax_query_attachments_args', $query);
    $query = new WP_Query($query);
    $posts = array_map('wp_prepare_attachment_for_js', $query->posts);
    $posts = array_filter($posts);
    wp_send_json_success($posts);
}