$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 );