wppaste
WordPress

WP_REST_Meta_Fields

Since
4.7.0
Source
wp-includes/rest-api/fields/class-wp-rest-meta-fields.php:15
Core class to manage meta values for an object via the REST API.

Compatibility

WordPress
since 4.7.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Methods · 17

Source code

#[AllowDynamicProperties]abstract class WP_REST_Meta_Fields { 	/**	 * Retrieves the object meta type.	 *	 * @since 4.7.0	 *	 * @return string One of 'post', 'comment', 'term', 'user', or anything	 *                else supported by `_get_meta_table()`.	 */	abstract protected function get_meta_type(); 	/**	 * Retrieves the object meta subtype.	 *	 * @since 4.9.8	 *	 * @return string Subtype for the meta type, or empty string if no specific subtype.	 */	protected function get_meta_subtype() {		return '';	} 	/**	 * Retrieves the object type for register_rest_field().	 *	 * @since 4.7.0	 *	 * @return string The REST field type, such as post type name, taxonomy name, 'comment', or `user`.	 */	abstract protected function get_rest_field_type(); 	/**	 * Registers the meta field.	 *	 * @since 4.7.0	 * @deprecated 5.6.0	 *	 * @see register_rest_field()	 */	public function register_field() {		_deprecated_function( __METHOD__, '5.6.0' ); 		register_rest_field(			$this->get_rest_field_type(),			'meta',			array(				'get_callback'    => array( $this, 'get_value' ),				'update_callback' => array( $this, 'update_value' ),				'schema'          => $this->get_field_schema(),			)		);	} 	/**	 * Retrieves the meta field value.	 *	 * @since 4.7.0	 *	 * @param int             $object_id Object ID to fetch meta for.	 * @param WP_REST_Request $request   Full details about the request.	 * @return array Array containing the meta values keyed by name.	 */	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 );

Changelog

Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.