wp_ajax_query_attachments()
- Since
- 3.5.0
- Source
wp-admin/includes/ajax-actions.php:3021
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
- Scaling
- Scales with input
- Instructions
- 88–124
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via ->query().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 137.
Third-party callbacks on 'ajax_query_attachments_args' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
current_user_can() && !isset($query) && !$total_posts | 88–104 | current_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_posts | 90–106 | current_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_posts | 92–108 | current_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_posts | 93–109 | current_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_posts | 94–110 | current_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_posts | 95–111 | current_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_posts | 97–113 | current_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_posts | 97–113 | current_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_posts | 99–115 | current_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_posts | 99–115 | current_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_posts | 101–117 | current_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_posts | 102–118 | current_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_posts | 103–119 | current_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_posts | 104–120 | current_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_posts | 106–122 | current_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_posts | 108–124 | current_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 137 | 88–124 | 13 | |
| 8.5 | 137 | 88–124 | 13 | |
| 8.4 | 137 | 88–124 | 13 | |
| 8.3 | 137 | 88–124 | 13 | |
| 8.2 | 137 | 88–124 | 13 | |
| 8.1 | 137 | 88–124 | 13 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 140 | 89–127 | 13 |
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:
- apply_filters( ajax_query_attachments_args )filterline 3080 (+59 into the body)
Filters the arguments passed to WP_Query during an Ajax call for querying attachments.
Uses · 9
- current_user_can()Returns whether the current user has the specified capability.
- wp_send_json_error()Sends a JSON response back to an Ajax request, indicating failure.
- get_taxonomies_for_attachments()Retrieves all of the taxonomies that are registered for attachments.
- get_post_type_object()Retrieves a post type object by name.
- add_filter()Adds a callback function to a filter hook.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- update_post_parent_caches()Updates parent post caches for a list of post objects.
- wp_send_json_success()Sends a JSON response back to an Ajax request, indicating success.
- WP_Query::__construct()Constructor.
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 $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;Changelog
Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.