WP_REST_Attachments_Controller::prepare_items_queryprotectedWP 4.7.0

Determines the allowed query_vars for a get_items() response and prepares for WP_Query.

Method of the class: WP_REST_Attachments_Controller{}

No Hooks.

Returns

Array. Array of query arguments.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_items_query( $prepared_args, $request );
$prepared_args(array)
Array of prepared arguments.
Default: empty array
$request(WP_REST_Request)
Request to prepare items for.
Default: null

Changelog

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

WP_REST_Attachments_Controller::prepare_items_query() code WP 6.9.1

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;
}