WP_REST_Attachments_Controller::prepare_items_query │ protected │ WP 4.7.0

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

Метод класса: WP_REST_Attachments_Controller{}

Хуков нет.

Возвращает

array. Array of query arguments.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_items_query( $prepared_args, $request );
$prepared_args(массив)
Array of prepared arguments.
По умолчанию: empty array
$request(WP_REST_Request)
Request to prepare items for.
По умолчанию: null

Список изменений

С версии 4.7.0 Введена.
С версии 6.9.0 Extends the media_type and mime_type request arguments to support array values.

Код WP_REST_Attachments_Controller::prepare_items_query() WP 7.1.4

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