Query examplesCustom Categories
Custom Categories
Read more in guide Working with Custom Categories.
These are examples of queries to fetch custom category taxonomy data.
Taxonomies unmapped to the schema
Retrieve categories with taxonomy "product-category"
:
query {
categories(taxonomy: "product-category") {
__typename
...on Category {
count
description
id
name
slug
url
}
...on GenericCategory {
taxonomy
customPostCount
customPosts {
__typename
...on CustomPost {
id
title
}
}
}
}
}
Retrieving categories associated to a custom post
Type GenericCustomPost
has field categories
, to retrieve the custom categories added to the custom post:
query {
customPosts(
filter: { customPostTypes: "product" }
) {
__typename
... on GenericCustomPost {
categories(taxonomy: "product-cat") {
__typename
id
name
taxonomy
}
}
}
}
Filtering custom posts by category
To retrieve the custom posts with given categories, we can use input filter.categories
:
query {
customPostsByCatIDs: customPosts(
filter: {
categories: {
includeBy: {
ids: [26, 28]
}
taxonomy: "product-cat"
}
}
) {
id
title
}
customPostsByCatSlugs: customPosts(
filter: {
categories: {
includeBy: {
slugs: ["news", "sports"]
}
taxonomy: "product-cat"
}
}
) {
id
title
}
}
Setting categories on a custom post
Mutation:
mutation {
setCategoriesOnCustomPost(
input: {
id: 1499,
categoryIDs: [2, 5]
taxonomy: "cat-taxonomy"
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
customPostID
customPost {
categories(taxonomy: "cat-taxonomy") {
id
}
categoryNames(taxonomy: "cat-taxonomy")
}
}
}
Nested mutation:
mutation {
customPost(by: { id: 1499 }) {
setCategories(
input: {
categoryIDs: [2, 5]
taxonomy: "cat-taxonomy"
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
customPostID
customPost {
categories(taxonomy: "cat-taxonomy") {
id
}
categoryNames(taxonomy: "cat-taxonomy")
}
}
}
}
Creating, updating and deleting a custom category
This query creates, updates and deletes category terms for a custom some-cat-taxonomy
category:
mutation CreateUpdateDeleteCategories {
createCategory(input: {
taxonomy: "some-cat-taxonomy",
name: "Some name"
slug: "Some slug"
description: "Some description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...CategoryData
}
}
updateCategory(input: {
id: 1
taxonomy: "some-cat-taxonomy"
name: "Some updated name"
slug: "Some updated slug"
description: "Some updated description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...CategoryData
}
}
deleteCategory(input: {
id: 1
taxonomy: "some-cat-taxonomy"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
}
fragment CategoryData on Category {
id
name
slug
description
parent {
id
}
}
Prev
Next