get_media_items( int $post_id, array $errors ): string
- Since
- 2.5.0
- Source
wp-admin/includes/media.php:1547
Description
The HTML markup retrieved will be created for the progress of SWF Upload component. Will also create link for showing and hiding the form to modify the image attachment.
Compatibility
- WordPress
- since 2.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.
Parameters
$post_idint- Post ID.
$errorsarray- Errors for attachment, if any.
Return value
string- HTML content for media items of post gallery.
Performance profile
How much work a call to get_media_items() 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
- 13–27
- Plugin surface
- None
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 70.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()one call below get_media_items()
Further down the call graph this can also reach cache, option, 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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_media_items() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–19 | none |
| always | 21–22 | get_post() |
| always | 23–27 | get_post(), post_parent() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 70 | 13–27 | 11 | |
| 8.5 | 70 | 13–27 | 11 | |
| 8.4 | 70 | 13–27 | 11 | |
| 8.3 | 70 | 13–27 | 11 | |
| 8.2 | 70 | 13–27 | 11 | |
| 8.1 | 70 | 13–27 | 11 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 72 | 14–27 | 11 |
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.
Uses · 3
- get_post()Retrieves post data given a post ID or post object.
- get_children()Retrieves all children of the post parent ID.
- get_media_item()Retrieves HTML form for modifying the image attachment.
Used by · 3
- media_upload_gallery_form()Adds gallery form to upload iframe.
- media_upload_library_form()Outputs the legacy media upload form for the media library.
- media_upload_type_form()Outputs the legacy media upload form for a given media type.
Source code
function get_media_items( $post_id, $errors ) { $attachments = array(); if ( $post_id ) { $post = get_post( $post_id ); if ( $post && 'attachment' === $post->post_type ) { $attachments = array( $post->ID => $post ); } else { $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC', ) ); } } else { if ( is_array( $GLOBALS['wp_the_query']->posts ) ) { foreach ( $GLOBALS['wp_the_query']->posts as $attachment ) { $attachments[ $attachment->ID ] = $attachment; } } } $output = ''; foreach ( (array) $attachments as $id => $attachment ) { if ( 'trash' === $attachment->post_status ) { continue; } $item = get_media_item( $id, array( 'errors' => $errors[ $id ] ?? null ) ); if ( $item ) { $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress hidden'><div class='bar'></div></div><div id='media-upload-error-$id' class='hidden'></div><div class='filename hidden'></div>$item\n</div>"; } } return $output;}Changelog
Introduced in 2.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 7.1.0 tag, from
src/wp-admin/includes/media.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.