wppaste
WordPress

wp_ajax_query_attachments()

Since
3.5.0
Source
wp-admin/includes/ajax-actions.php:3028
Handles querying attachments via AJAX.

Compatibility

WordPress
since 3.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to wp_ajax_query_attachments() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->query().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
88–124

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 137.

Plugin surface
1 hook

Third-party callbacks on 'ajax_query_attachments_args' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->query()called directly

Further down the call graph this can also reach option, query, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_query_attachments() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
current_user_can() && !isset($query) && !$total_posts88–104current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), header(), header(), wp_send_json_success()
!current_user_can() && !isset($query) && !$total_posts90–106current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), header(), header(), wp_send_json_success()
current_user_can() && isset($query) && !$total_posts92–108current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), header(), header(), wp_send_json_success()
current_user_can() && !isset($query) && !$total_posts93–109current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), ceil(), header(), header(), wp_send_json_success()
!current_user_can() && isset($query) && !$total_posts94–110current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), header(), header(), wp_send_json_success()
!current_user_can() && !isset($query) && !$total_posts95–111current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), ceil(), header(), header(), wp_send_json_success()
current_user_can() && !isset($query) && $total_posts97–113current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), header(), header(), wp_send_json_success()
current_user_can() && isset($query) && !$total_posts97–113current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), ceil(), header(), header(), wp_send_json_success()
!current_user_can() && !isset($query) && $total_posts99–115current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), header(), header(), wp_send_json_success()
!current_user_can() && isset($query) && !$total_posts99–115current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->get(), ceil(), header(), header(), wp_send_json_success()
current_user_can() && isset($query) && $total_posts101–117current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), header(), header(), wp_send_json_success()
current_user_can() && !isset($query) && $total_posts102–118current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), ceil(), header(), header(), wp_send_json_success()
4 further outcomes, up to 124 instructions
!current_user_can() && isset($query) && $total_posts103–119current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), header(), header(), wp_send_json_success()
!current_user_can() && !isset($query) && $total_posts104–120current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), ceil(), header(), header(), wp_send_json_success()
current_user_can() && isset($query) && $total_posts106–122current_user_can(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), ceil(), header(), header(), wp_send_json_success()
!current_user_can() && isset($query) && $total_posts108–124current_user_can(), wp_send_json_error(), get_taxonomies_for_attachments(), array_flip(), array_intersect_key(), get_post_type_object(), current_user_can(), add_filter(), apply_filters(), update_post_parent_caches(), array_map(), array_filter(), ->query(), ->get(), ceil(), header(), header(), wp_send_json_success()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13788–12413
8.513788–12413
8.413788–12413
8.313788–12413
8.213788–12413
8.113788–124133 fewer instructions than PHP 7.4
7.414089–12713

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_ajax_query_attachments() runs, in this order:

  1. apply_filters( ajax_query_attachments_args )filterline 3087 (+59 into the body)

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

Uses · 9

Source code

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 $taxonomy ) {		if ( $taxonomy->query_var && isset( $query[ $taxonomy->query_var ] ) ) {			$keys[] = $taxonomy->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;

Changelog

Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-admin/includes/ajax-actions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.