WP_REST_Meta_Fields::get_value() WordPress Method

The WP_REST_Meta_Fields::get_value() method is used to retrieve the value of a given meta field for a given object. The method takes two arguments: the ID of the object and the key of the meta field. If the meta field does not exist, the method will return false. Otherwise, it will return the value of the meta field.

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

Retrieves the meta field value.


Parameters

$object_id

(int)(Required)Object ID to fetch meta for.

$request

(WP_REST_Request)(Required)Full details about the request.


Top ↑

Return

(array) Array containing the meta values keyed by name.


Top ↑

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();

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

			$response[ $name ] = $value;
		}

		return $response;
	}


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.