WP_REST_Meta_Fields::get_value

Advertisement

Summery Summery

Retrieves the meta field value.

Syntax Syntax

WP_REST_Meta_Fields::get_value( int $object_id, WP_REST_Request $request )

Parameters Parameters

$object_id

(Required) Object ID to fetch meta for.

$request

(Required) Full details about the request.

Return Return

(object|WP_Error) Object containing the meta values by name, otherwise WP_Error object.

Source Source

File: wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

	public function get_value( $object_id, $request ) {
		$fields   = $this->get_registered_fields();
		$response = array();

		foreach ( $fields as $meta_key => $args ) {
			$name       = $args['name'];
			$all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false );

			if ( $args['single'] ) {
				if ( empty( $all_values ) ) {
					$value = $args['schema']['default'];
				} else {
					$value = $all_values[0];
				}

				$value = $this->prepare_value_for_response( $value, $request, $args );
			} else {
				$value = array();

				foreach ( $all_values as $row ) {
					$value[] = $this->prepare_value_for_response( $row, $request, $args );
				}
			}

			$response[ $name ] = $value;
		}

		return $response;
	}

Advertisement

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Advertisement

Leave a Reply