How to read, create, edit and delete category and term data in WordPress via REST API

How to read, create, edit and delete category and term data in WordPress via REST API

We continue our series of articles dedicated to the REST WordPress API. Today, we’ll learn how to read, create, update, and delete category and term data (this also applies to custom taxonomies, which we’ll cover later).

Working with terms is almost identical to working with posts, as shown in the previous article. The only differences are the endpoint URL and the parameters used for retrieving, creating, or updating data.

How to get a list of categories via WordPress REST API

To retrieve category or tag data via API, use the following code:

$url = 'http://мой-сайт/wp-json/wp/v2/categories';
$response = wp_remote_get($url, [
	'body' => [
		'include' => [12, 13, 18],
	]
]);
$records = json_decode(
	wp_remote_retrieve_body($response), 
	true
);

echo '<strong>Code:</strong> ' . wp_remote_retrieve_response_code($response);
echo '<br>';
echo '<strong>Message:</strong> ' . wp_remote_retrieve_response_message($response);
echo '<br>';
echo '<strong>Records:</strong> ' . count($records);
echo '<br>';
echo '<strong>Answer:</strong>';
echo "<pre>\$records = ";print_r($records);echo"</pre>";
die;

Let’s break down the code line by line:
wp_remote_get — performs a CURL request from one WP site to another.
wp_remote_retrieve_body — the response contains not only data (html/txt/json/raw) but also headers and metadata, so we extract just the body content.
json_decode — we decode the JSON response into a PHP array.
$records — holds the resulting data array.
wp_remote_retrieve_response_code — returns the HTTP status code.
wp_remote_retrieve_response_message — returns the HTTP response message.

 

How to create a tag or category using the REST API in WordPress

Remember, to create, update, or delete data via the API, authentication is required. You’ll need the “JSON Basic Authentication” plugin installed.

To create a category, run the following code:

/** Create Category */
$url = 'http://мой-сайт/wp-json/wp/v2/categories';
$username = 'admin';
$password = 'qweqwe';
$headers = [
	'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
];

$response = wp_remote_post($url, [
	'headers' => $headers,
	'body' => [
		'name' => 'Test name category',
		'description' => 'Test description category',
		'slug' => 'test-name-category',
	]
]);
$records = json_decode(wp_remote_retrieve_body($response), true);
echo"<pre>";print_r($records);echo"</pre>";
die;

Where:
$username — the admin login.
$password — the admin password.
$headers — HTTP headers containing Basic Auth credentials.
wp_remote_post — a wrapper function over CURL for sending POST data.
$records — the server's JSON response. On success, it returns the created category or tag data.

Creating a WordPress tag via the API is similar to creating a category:

/** Creating Tags */
$url = 'http://мой-сайт/wp-json/wp/v2/tags';
$response = wp_remote_post($url, [
	'headers' => $headers,
	'body' => [
		'name' => 'Test name tag',
		'description' => 'Test description tag',
		'slug' => 'test-name-tag',
	]
]);
$records = json_decode(wp_remote_retrieve_body($response), true);
echo"<pre>";print_r($records);echo"</pre>";
die;

The only difference is the URL you send the data to.

How to update a category or tag via the WordPress REST API

We’ll show an example for a category (updating a tag is similar, you only need to change “categories” to “tags” in the URL):

$url = 'http://my-site/wp-json/wp/v2/categories/34';
$response = wp_remote_post($url, [
	'headers' => $headers,
	'body' => [
		'name' => 'Test name category (updated: '.date('Y-m-d H:i:s').')',
	]
]);
$records = json_decode(wp_remote_retrieve_body($response), true);
echo"<pre>";print_r($records);echo"</pre>";
die;

Note the URL:

$url = 'http://my-site/wp-json/wp/v2/categories/34';

At the end, 34 is the ID of the category you want to update.

You can update all the same fields used during creation: name, description, slug, and parent.

How to delete a category or tag via the WordPress REST API

Here’s an example of deleting a tag (deleting a category works the same, just use “categories” in the URL instead of “tags”):

$url = 'http://мой-сайт/wp-json/wp/v2/tags/31';
$response = wp_remote_request($url, [
	'method' => 'DELETE',
	'headers' => $headers,
	'body' => [
		'force' => true,
	]
]);
$records = json_decode(wp_remote_retrieve_body($response), true);
echo"<pre>";print_r($records);echo"</pre>";
die;

The "force" => true parameter inside the body array is required. It ensures the term is permanently deleted, bypassing the trash (since terms don’t support trash). Omitting it may result in an error.

The $records variable will contain the data of the term that was just deleted.

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 *