WP_REST_Meta_Fields::delete_meta_value() WordPress Method

The WP_REST_Meta_Fields::delete_meta_value() function deletes the value of a given meta key for a given object. The function takes two arguments: the object ID and the meta key.

WP_REST_Meta_Fields::delete_meta_value( int $object_id, string $meta_key, string $name ) #

Deletes a meta value for an object.


Parameters

$object_id

(int)(Required)Object ID the field belongs to.

$meta_key

(string)(Required)Key for the field.

$name

(string)(Required)Name for the field that is exposed in the REST API.


Top ↑

Return

(true|WP_Error) True if meta field is deleted, WP_Error otherwise.


Top ↑

Source

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

	protected function delete_meta_value( $object_id, $meta_key, $name ) {
		$meta_type = $this->get_meta_type();

		if ( ! current_user_can( "delete_{$meta_type}_meta", $object_id, $meta_key ) ) {
			return new WP_Error(
				'rest_cannot_delete',
				/* translators: %s: Custom field key. */
				sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.' ), $name ),
				array(
					'key'    => $name,
					'status' => rest_authorization_required_code(),
				)
			);
		}

		if ( null === get_metadata_raw( $meta_type, $object_id, wp_slash( $meta_key ) ) ) {
			return true;
		}

		if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $meta_key ) ) ) {
			return new WP_Error(
				'rest_meta_database_error',
				__( 'Could not delete meta value from database.' ),
				array(
					'key'    => $name,
					'status' => WP_Http::INTERNAL_SERVER_ERROR,
				)
			);
		}

		return true;
	}


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.