Custom Categories

We can add categories to posts in WordPress (i.e. using the taxonomy with name "category"). This is already mapped in the GraphQL schema via the PostCategory type, associated to a Post entry.

Likewise, a custom post type, defined by any theme or plugin (such as "product"), can have its own taxonomy category associated to it (such as "product-cat"). As these custom post types are not mapped to the GraphQL schema, they are resolved via type GenericCustomPost, and their categories are resolved as GenericCategory.

We use fields category and categories to fetch category data, which must indicate which taxonomy they refer to via mandatory field argument taxonomy. The result is of the union type CategoryUnion, which includes entries from either PostCategory or GenericCategory (depending on the entry's taxonomy).

For instance, this query retrieves categories with taxonomy "product-category":

{
categories(taxonomy: "product-category") {
__typename

...on Category {
count
description
id
name
slug
url
}

...on GenericCategory {
taxonomy
customPostCount
customPosts {
__typename
...on CustomPost {
id
title
}
}
}
}
}

Allowing access to unmapped category taxonomies permalink

The category taxonomies accessible via the GenericCategory type must be explicitly configured in the plugin Settings page, as explained in guide Adding a custom category taxonomy to the schema.

Retrieving categories associated to a custom post permalink

Type GenericCustomPost has field categories, to retrieve the custom categories added to the custom post:

{
customPosts(
filter: { customPostTypes: "product" }
) {
__typename

... on GenericCustomPost {
categories(taxonomy: "product-cat") {
__typename
id
name
taxonomy
}
}
}
}

Filtering custom posts by category permalink

To retrieve the custom posts with a given category or categories, we can use inputs filter.categoryIDs and filter.categoryTaxonomy:

{
customPostsByCat: customPosts(
filter: {
categoryIDs: [26, 28],
categoryTaxonomy: "product-category"
}
) {
id
title
}
}