WP_REST_Attachments_Controller::prepare_items_query() WordPress Method

The WP_REST_Attachments_Controller::prepare_items_query() method is used to prepare the query for retrieving attachments. This is done by setting up the necessary arguments for WP_Query.

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

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


Parameters

$prepared_args

(array)(Optional) Array of prepared arguments.

Default value: array()

$request

(WP_REST_Request)(Optional) Request to prepare items for.

Default value: null


Top ↑

Return

(array) Array of query arguments.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

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

		$media_types = $this->get_media_types();

		if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) {
			$query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
		}

		if ( ! empty( $request['mime_type'] ) ) {
			$parts = explode( '/', $request['mime_type'] );
			if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) {
				$query_args['post_mime_type'] = $request['mime_type'];
			}
		}

		// Filter query clauses to include filenames.
		if ( isset( $query_args['s'] ) ) {
			add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
		}

		return $query_args;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.