How to Get, Create, Edit, or Delete Metadata in WordPress via REST API

How to Get, Create, Edit, or Delete Metadata in WordPress via REST API

Today we’ll take a look at another useful feature — how to work with post or term meta data via the API. Just like with creating data via the API, WordPress by default doesn’t support meta field interaction. To enable it, we’ll need to install a third-party plugin. Unfortunately, I couldn’t find it on GitHub — maybe it was moved or merged into the WordPress plugin repository. However, since I still have the plugin archive locally, we’ll use it for this article (you can download the plugin via the provided link). I’m sure you can also find a similar plugin in the WP repository with good reviews.

How to get post meta data via REST API

Just like in the previous articles on this topic, the code will be almost identical:

$url = //мой-сайт/wp-json/wp/v2/posts/2694/meta';
$response = wp_remote_get($url, [
	'headers' => $headers,
]);
$records = json_decode(
	wp_remote_retrieve_body($response), 
	true
);
echo"<pre style='dir:ltr;text-align:left'>";print_r($records);echo"</pre>";
die;

Take note of the URL — just before the final meta segment, we include the post ID whose meta fields we want to retrieve.
Also, it’s essential to send authentication headers with the request (see the previous articles for reference).

The response will contain a list of all meta fields. Note that only “public” fields will be returned — i.e. those whose keys do NOT begin with an underscore _.

How to get meta data for categories in WordPress

The difference between retrieving meta for posts and for categories (or tags, or other terms) is only in the request URL.
For posts, the URL looks like:
//my-site/wp-json/wp/v2/posts/{post_ID}/meta

For categories:
//my-site/wp-json/wp/v2/categories/{category_ID}/meta

For tags:
//my-site/wp-json/wp/v2/tags/{tag_ID}/meta

And for any other taxonomy term:
//my-site/wp-json/wp/v2/{taxonomy_name}/{term_ID}/meta

Example:

$username = 'admin';
$password = 'qweqwe';
$headers = [
	'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
];
$response = wp_remote_get('//мой-сайт/wp-json/wp/v2/categories/14/meta', [
	'headers' => $headers,
]);

How to add meta data to a post using the WordPress REST API

To add new meta data via the API, use the following code:

$url = '//мой-сайт/wp-json/wp/v2/posts/2694/meta';
$response = wp_remote_post($url, [
	'headers' => $headers,
	'body' => [
		'key' => 'blue',
		'value' => 'lightsaber_color',
	]
]);
$records = json_decode(
	wp_remote_retrieve_body($response), 
	true
);
echo"<pre style='dir:ltr;text-align:left'>";print_r($records);echo"</pre>";
die;

As in previous examples, you must send authentication headers in the request (the headers array). In the body, you provide the meta key and value.
Note the URL — it must include the ID of the post to which the meta field is being added. The request method must be POST.

Updating WordPress meta fields via REST API

To modify an existing meta field, the request URL must include both the post ID and the meta field ID. You can change both the meta key and its value — which is quite convenient:

$url = '//мой-сайт/wp-json/wp/v2/posts/2694/meta/11723';
$response = wp_remote_post($url, [
	'headers' => $headers,
	'body' => [
		'key' => 'color',
		'value' => 'red',
	]
]);
$records = json_decode(
	wp_remote_retrieve_body($response), 
	true
);
echo"<pre style='dir:ltr;text-align:left'>";print_r($records);echo"</pre>";
die;

You can find out the meta field ID by performing a request to retrieve all post meta fields.

How to delete WordPress meta data via REST API

To delete a meta field, use the same URL as for editing, but the request method must be DELETE:

$url = '//мой-сайт/wp-json/wp/v2/posts/2694/meta/11723';
$response = wp_remote_request($url, [
	'method' => 'DELETE',
	'headers' => $headers,
	'body' => [
		'force' => true,
	]
]);

The response will contain a message indicating that the specified meta field was deleted.

That’s all.

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *