Query WordPress dataPost Categories
Post Categories
These are examples of queries to fetch post category data.
Fetching categories
List of post categories, ordering them by name, and showing their post count:
query {
postCategories(
sort: { order: ASC, by: NAME }
pagination: { limit: 50 }
) {
id
name
url
postCount
}
}
All categories in a post:
query {
post(by: { id: 1 }) {
categories {
id
name
url
}
}
}
Category names in posts:
query {
posts {
id
title
categoryNames
}
}
A list of predefined categories:
query {
postCategories(filter: { ids: [2, 5] }) {
id
name
url
}
}
Filtering categories by name:
query {
postCategories(filter: { search: "rr" }) {
id
name
url
}
}
Counting category results:
query {
postCategoryCount(filter: { search: "rr" })
}
Paginating categories:
query {
postCategories(
pagination: {
limit: 3,
offset: 3
}
) {
id
name
url
}
}
Only top-level categories, and 2nd level of children:
{
postCategories(pagination: { limit: 50 }, filter: { parentID: 0 }) {
...CatProps
children {
...CatProps
children {
...CatProps
}
}
}
}
fragment CatProps on PostCategory {
id
name
parent {
id
name
}
childNames
childCount
}
Fetching meta values:
query {
postCategories(
pagination: { limit: 5 }
) {
id
name
metaValue(
key: "someKey"
)
}
}
Setting categories on a post
Mutation:
mutation {
setCategoriesOnPost(
input: {
id: 1499,
categoryIDs: [2, 5]
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
postID
post {
categories {
id
}
categoryNames
}
}
}
Nested mutation:
mutation {
post(by: { id: 1499 }) {
setCategories(
input: {
categoryIDs: [2, 5]
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
postID
post {
categories {
id
}
categoryNames
}
}
}
}
Creating, updating and deleting a post category
This query creates, updates and deletes post category terms:
mutation CreateUpdateDeletePostCategories {
createPostCategory(input: {
name: "Some name"
slug: "Some slug"
description: "Some description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...PostCategoryData
}
}
updatePostCategory(input: {
id: 1
name: "Some updated name"
slug: "Some updated slug"
description: "Some updated description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...PostCategoryData
}
}
deletePostCategory(input: {
id: 1
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
}
fragment PostCategoryData on PostCategory {
id
name
slug
description
parent {
id
}
}
Prev
Next