These routes allow you to retrieve/create/update/delete categories.
Possible routes (endpoints are described below):
/wp/v2/categories // works with all categories
/wp/v2/categories/{ID} // works with the specified category instead of {ID}
Controller class: WP_REST_Terms_Controller{}
Resource schema
The schema shows all fields that exist for the object: the fields of the object that the request will return.
Parameter
Context
Description
id number
view, edit, embed
Term ID (category). Read-only.
count number
view, edit
Number of posts in the term (category). Read-only.
description string
view, edit
Description of the term (category).
link string, uri
view, edit, embed
URL of the term (category). Read-only.
name string
view, edit, embed
Name of the term (category).
slug string
view, edit, embed
Slug of the term (category), usually created from the name.
taxonomy string
view, edit, embed
Name of the taxonomy. Read-only. Can be: category , post_tag , nav_menu , link_category , post_format
parent number
view, edit
ID of the parent term.
meta object
view, edit
Meta fields.
Context — shows which fields of the object will be returned in the response when creating a request in the specified context. For example, when updating or creating a category, the fields corresponding to the edit context will be returned.
wp/v2/categories
An OPTIONS request to the route will return a complete description of this route: endpoints, their parameters, schema.
$ curl -X OPTIONS -i http://demo.wp-api.org/wp-json/wp/v2/categories
GitHub
{
"namespace": "wp/v2",
"methods": [
"GET",
"POST"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"context": {
"required": false,
"default": "view",
"enum": [
"view",
"embed",
"edit"
],
"description": "Рамки в которых сделан запрос, определяют поля в ответе.",
"type": "string"
},
"page": {
"required": false,
"default": 1,
"description": "Текущая страница коллекции.",
"type": "integer"
},
"per_page": {
"required": false,
"default": 10,
"description": "Максимальное число объектов возвращаемое в выборке.",
"type": "integer"
},
"search": {
"required": false,
"description": "Ограничить результаты до совпадающих со строкой.",
"type": "string"
},
"exclude": {
"required": false,
"default": [],
"description": "Убедиться что выборка исключает определенные ID.",
"type": "array",
"items": {
"type": "integer"
}
},
"include": {
"required": false,
"default": [],
"description": "Ограничить выборку до определенных ID.",
"type": "array",
"items": {
"type": "integer"
}
},
"order": {
"required": false,
"default": "asc",
"enum": [
"asc",
"desc"
],
"description": "Упорядочить сортировку атрибута по возрастанию или убыванию.",
"type": "string"
},
"orderby": {
"required": false,
"default": "name",
"enum": [
"id",
"include",
"name",
"slug",
"include_slugs",
"term_group",
"description",
"count"
],
"description": "Сортировать коллекцию по атрибутам элемента.",
"type": "string"
},
"hide_empty": {
"required": false,
"default": false,
"description": "Скрывать ли элементы не назначенные ни одной записи.",
"type": "boolean"
},
"parent": {
"required": false,
"description": "Ограничить выборку элементами назначенными определенному родителю.",
"type": "integer"
},
"post": {
"required": false,
"description": "Ограничить выборку элементами назначенными определенной записи.",
"type": "integer"
},
"slug": {
"required": false,
"description": "Ограничить выборку элементами с одним или более специальными ярлыками. ",
"type": "array",
"items": {
"type": "string"
}
}
}
},
{
"methods": [
"POST"
],
"args": {
"description": {
"required": false,
"description": "HTML описание элемента.",
"type": "string"
},
"name": {
"required": true,
"description": "HTML название элемента.",
"type": "string"
},
"slug": {
"required": false,
"description": "Буквенно-цифровой идентификатор элемента уникальный для его типа.",
"type": "string"
},
"parent": {
"required": false,
"description": "ID элемента родителя.",
"type": "integer"
},
"meta": {
"required": false,
"description": "Мета поля.",
"type": "object"
}
}
}
],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "category",
"type": "object",
"properties": {
"id": {
"description": "Уникальный идентификатор элемента.",
"type": "integer",
"context": [
"view",
"embed",
"edit"
],
"readonly": true
},
"count": {
"description": "Число опубликованных записей элемента.",
"type": "integer",
"context": [
"view",
"edit"
],
"readonly": true
},
"description": {
"description": "HTML описание элемента.",
"type": "string",
"context": [
"view",
"edit"
]
},
"link": {
"description": "URL элемента.",
"type": "string",
"format": "uri",
"context": [
"view",
"embed",
"edit"
],
"readonly": true
},
"name": {
"description": "HTML название элемента.",
"type": "string",
"context": [
"view",
"embed",
"edit"
],
"required": true
},
"slug": {
"description": "Буквенно-цифровой идентификатор элемента уникальный для его типа.",
"type": "string",
"context": [
"view",
"embed",
"edit"
]
},
"taxonomy": {
"description": "Тип атрибуции элемента.",
"type": "string",
"enum": [
"category",
"post_tag",
"nav_menu",
"link_category",
"post_format"
],
"context": [
"view",
"embed",
"edit"
],
"readonly": true
},
"parent": {
"description": "ID элемента родителя.",
"type": "integer",
"context": [
"view",
"edit"
]
},
"meta": {
"description": "Мета поля.",
"type": "object",
"context": [
"view",
"edit"
],
"properties": []
}
}
},
"_links": {
"self": "http://wptest.ru/wp-json/wp/v2/categories"
}
}
List of categories
Retrieves a list of categories, similar to get_categories() .
Request type
GET /wp/v2/categories
Request parameters
context
The area in which the request is made; defines the fields present in the response.
Can be: view, embed, edit
Default: view
page
The current pagination page.
Default: 1
per_page
The maximum number of items returned in the result.
Default: 10
search
Limit results to those that match the string.
exclude
Excludes categories by ID.
include
Shows only the specified categories. ID is specified.
orderby
Which field to sort by?
Can be: id , include , name , slug , term_group , description , count
Default: name
order
How to sort?
Can be: asc, desc
Default: asc
hide_empty
Hide empty categories (where there are no posts)?
parent
ID of the parent category whose children need to be retrieved.
post
ID of the post whose categories need to be retrieved.
slug
Specify the slugs of the categories to be retrieved.
Example request
http://demo.wp-api.org/wp-json/wp/v2/categories
Get categories of post 1
http://demo.wp-api.org/wp-json/wp/v2/categories?post=1
Creating a category
The algorithm is similar to the operation of the function wp_insert_category() .
Request type
Access: authorization required
POST /wp/v2/categories
Request parameters
name(required)
Name of the term (category).
description
Description of the term (category).
slug
Slug of the term (category), usually created from the name.
parent
ID of the parent term.
meta
Meta fields.
Example request:
POST http://example.com/wp-json/wp/v2/categories?name=My category
Response
{
"id": 6,
"count": 0,
"description": "",
"link": "http://example.com/cat/my-category/",
"name": "My category",
"slug": "my-category",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "http://example.com/wp-json/wp/v2/categories/6"
}
],
"collection": [
{
"href": "http://example.com/wp-json/wp/v2/categories"
}
],
"about": [
{
"href": "http://example.com/wp-json/wp/v2/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "http://example.com/wp-json/wp/v2/posts?categories=6"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
wp/v2/categories/{id}
An OPTIONS request to the route will return a complete description of this route: endpoints, their parameters, schema.
$ curl -X OPTIONS -i http://demo.wp-api.org/wp-json/wp/v2/categories/1
GitHub
{
"namespace": "wp/v2",
"methods": [
"GET",
"POST",
"PUT",
"PATCH",
"DELETE"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"id": {
"required": false,
"description": "Уникальный идентификатор элемента.",
"type": "integer"
},
"context": {
"required": false,
"default": "view",
"enum": [
"view",
"embed",
"edit"
],
"description": "Рамки в которых сделан запрос, определяют поля в ответе.",
"type": "string"
}
}
},
{
"methods": [
"POST",
"PUT",
"PATCH"
],
"args": {
"id": {
"required": false,
"description": "Уникальный идентификатор элемента.",
"type": "integer"
},
"description": {
"required": false,
"description": "HTML описание элемента.",
"type": "string"
},
"name": {
"required": false,
"description": "HTML название элемента.",
"type": "string"
},
"slug": {
"required": false,
"description": "Буквенно-цифровой идентификатор элемента уникальный для его типа.",
"type": "string"
},
"parent": {
"required": false,
"description": "ID элемента родителя.",
"type": "integer"
},
"meta": {
"required": false,
"description": "Мета поля.",
"type": "object"
}
}
},
{
"methods": [
"DELETE"
],
"args": {
"id": {
"required": false,
"description": "Уникальный идентификатор элемента.",
"type": "integer"
},
"force": {
"required": false,
"default": false,
"description": "Должно быть истинно, так как элементы не поддерживают перемещение в корзину.",
"type": "boolean"
}
}
}
],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "category",
"type": "object",
"properties": {
"id": {
"description": "Уникальный идентификатор элемента.",
"type": "integer",
"context": [
"view",
"embed",
"edit"
],
"readonly": true
},
"count": {
"description": "Число опубликованных записей элемента.",
"type": "integer",
"context": [
"view",
"edit"
],
"readonly": true
},
"description": {
"description": "HTML описание элемента.",
"type": "string",
"context": [
"view",
"edit"
]
},
"link": {
"description": "URL элемента.",
"type": "string",
"format": "uri",
"context": [
"view",
"embed",
"edit"
],
"readonly": true
},
"name": {
"description": "HTML название элемента.",
"type": "string",
"context": [
"view",
"embed",
"edit"
],
"required": true
},
"slug": {
"description": "Буквенно-цифровой идентификатор элемента уникальный для его типа.",
"type": "string",
"context": [
"view",
"embed",
"edit"
]
},
"taxonomy": {
"description": "Тип атрибуции элемента.",
"type": "string",
"enum": [
"category",
"post_tag",
"nav_menu",
"link_category",
"post_format"
],
"context": [
"view",
"embed",
"edit"
],
"readonly": true
},
"parent": {
"description": "ID элемента родителя.",
"type": "integer",
"context": [
"view",
"edit"
]
},
"meta": {
"description": "Мета поля.",
"type": "object",
"context": [
"view",
"edit"
],
"properties": []
}
}
}
}
Getting a category
The algorithm is similar to the operation of the function get_category() .
Request type
GET /wp/v2/categories/{id}
Request parameters
id(number)
Identifier of the term.
context(string)
The area in which the request is made; defines the fields present in the response.
Can be: view, embed, edit
Default: view
Example request
http://demo.wp-api.org/wp-json/wp/v2/categories/25
Updating a category
The algorithm is similar to the operation of the function wp_update_category() .
Request type
POST /wp/v2/categories/{id}
Request parameters
id(required)
ID of the term (category).
name
Name of the term (category).
description
Description of the term (category).
slug
Slug of the term (category), usually created from the name.
parent
ID of the parent term.
meta
Meta fields.
Example request
$ curl -X POST http://demo.wp-api.org/wp-json/wp/v2/categories/25 -d '{"name":"New name"}'
Or
POST http://example.com/wp-json/wp/v2/categories/6?name=My category&slug=My category
Response
{
"id": 6,
"count": 0,
"description": "",
"link": "http://example.com/cat/my-category/",
"name": "My category",
"slug": "my-category",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "http://example.com/wp-json/wp/v2/categories/6"
}
],
"collection": [
{
"href": "http://example.com/wp-json/wp/v2/categories"
}
],
"about": [
{
"href": "http://example.com/wp-json/wp/v2/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "http://example.com/wp-json/wp/v2/posts?categories=6"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
Deleting a category
The algorithm is similar to the operation of the function wp_delete_term() .
Request type
DELETE /wp/v2/categories/{id}
Request parameters
id(required)
ID of the term (category).
force
Must always be true, as terms do not support trash.
Example request
$ curl -X DELETE http://example.com/wp-json/wp/v2/categories/6?force=true
Response
{
"deleted": true,
"previous": {
"id": 6,
"count": 0,
"description": "",
"link": "http://example.com/cat/my-category/",
"name": "My category",
"slug": "my-category",
"taxonomy": "category",
"parent": 0,
"meta": []
}
}
Response if such a category does not exist
{
"code": "rest_term_invalid",
"message": "The item does not exist.",
"data": {
"status": 404
}
}