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
- get_meta_type()Retrieves the object meta type.
- get_meta_subtype()Retrieves the object meta subtype.
- get_rest_field_type()Retrieves the object type for register_rest_field().
- register_field()Registers the meta field.
- get_value()Retrieves the meta field value.
- prepare_value_for_response()Prepares a meta value for a response.
- update_value()Updates meta values.
- delete_meta_value()Deletes a meta value for an object.
- update_multi_meta_value()Updates multiple meta values for an object.
- update_meta_value()Updates a meta value for an object.
- is_meta_value_same_as_stored_value()Checks if the user provided value is equivalent to a stored value for the given meta key.
- get_registered_fields()Retrieves all the registered meta fields.
- get_field_schema()Retrieves the object's meta schema, conforming to JSON Schema.
- prepare_value()Prepares a meta value for output.
- check_meta_is_array()Check the 'meta' value of a request is an associative array.
- default_additional_properties_to_false()Recursively add additionalProperties = false to all objects in a schema if no additionalProperties setting is specified.
- get_empty_value_for_type()Gets the empty value for a schema type.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.