wppaste
WordPress

WP_REST_Attachments_Controller::prepare_items_query( array $prepared_args = array(), WP_REST_Request $request = null ): array

Since
4.7.0, 6.9.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:79
Determines the allowed query_vars for a get_items() response and prepares for WP_Query.

Compatibility

WordPress
since 6.9.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

$prepared_argsarrayoptional
Array of prepared arguments. Default empty array.Default: array()
$requestWP_REST_Requestoptional
Request to prepare items for.Default: null

Return value

array
Array of query arguments.

Performance profile

How much work a call to WP_REST_Attachments_Controller::prepare_items_query() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
22–50

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
empty($all_mime_types) && !isset($query_args)22–38::prepare_items_query(), ->get_media_types()
empty($all_mime_types) && isset($query_args)26–42::prepare_items_query(), ->get_media_types(), add_filter()
!empty($all_mime_types) && !isset($query_args)30–46::prepare_items_query(), ->get_media_types(), array_unique(), array_values()
!empty($all_mime_types) && isset($query_args)34–50::prepare_items_query(), ->get_media_types(), array_unique(), array_values(), add_filter()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7522–5014
8.57522–5014
8.47522–50143 fewer instructions than PHP 8.3
8.37822–5014
8.27822–5014
8.17822–5014
7.47822–5014

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

Source code

	protected function prepare_items_query( $prepared_args = array(), $request = null ) {		$query_args = parent::prepare_items_query( $prepared_args, $request ); 		if ( empty( $query_args['post_status'] ) ) {			$query_args['post_status'] = 'inherit';		} 		$all_mime_types = array();		$media_types    = $this->get_media_types(); 		if ( ! empty( $request['media_type'] ) && is_array( $request['media_type'] ) ) {			foreach ( $request['media_type'] as $type ) {				if ( isset( $media_types[ $type ] ) ) {					$all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] );				}			}		} 		if ( ! empty( $request['mime_type'] ) && is_array( $request['mime_type'] ) ) {			foreach ( $request['mime_type'] as $mime_type ) {				$parts = explode( '/', $mime_type );				if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) {					$all_mime_types[] = $mime_type;				}			}		} 		if ( ! empty( $all_mime_types ) ) {			$query_args['post_mime_type'] = array_values( array_unique( $all_mime_types ) );		} 		// Filter query clauses to include filenames.		if ( isset( $query_args['s'] ) ) {			add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );		} 		return $query_args;	}

Changelog

Introduced in 4.7.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.

6.9.0
Extends the media_type and mime_type request arguments to support array values.from the docblock
4.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.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.