ajax_query_attachments_argsfilter-hookWP 3.7.0

Allows you to change the parameters passed to WP_Query during an AJAX request for media files (attachments).

Usage

add_filter( 'ajax_query_attachments_args', 'wp_kama_ajax_query_attachments_args_filter' );

/**
 * Function for `ajax_query_attachments_args` filter-hook.
 * 
 * @param array $query An array of query variables.
 *
 * @return array
 */
function wp_kama_ajax_query_attachments_args_filter( $query ){

	// filter...
	return $query;
}
$query(array)
An array of parameters for WP_Query.

Examples

#1 Correctly display a gallery in Carbon Fields in the admin area

Carbon Fields v3.1.2 had a problem: after uploading 20 photos, all of them were displayed, but after refreshing the page, the field showed only the number specified in the posts_per_page option. The field stored all specified photo IDs, but WP_Query() returned a limited number during the AJAX request in the admin area, rather than every photo specified in post__in. The task is to determine posts_per_page from the number of IDs passed in post__in:

add_filter( 'ajax_query_attachments_args', function ( $query ) {
	if ( isset( $query['post__in'] ) && is_array( $query['post__in'] ) ) {
		$query['posts_per_page'] = count( $query['post__in'] );
	}

	return $query;
} );

Changelog

Since 3.7.0 Introduced.

Where the hook is called

wp_ajax_query_attachments()
ajax_query_attachments_args
wp-admin/includes/ajax-actions.php 3108
$query             = apply_filters( 'ajax_query_attachments_args', $query );

Where the hook is used in WordPress

Usage not found.