Categories and Tags
Categories and Tags are core Taxonomy Types in WordPress and allow content to be grouped. Each individual Category or Tag is known as a "Term".
Terms are exposed to the WPGraphQL Schema by default. On this page are some examples of interacting with Categories and Tags using WPGraphQL.
WPGraphQL comes with RootQuery fields built-in to query Tags and Categories. You can query for lists of Terms, an individual term, and even Post Objects related to Terms.
List Of Tags
In this example, we query a list of Tag nodes. By default, WPGraphQL will return 10 items.GraphiQL Loading...
In this example, we query a list of Category nodes. By default, WPGraphQL will return 10 items.GraphiQL Loading...
Tags are associated with Posts (and possibly other Post Types). Any Post Type that is registered to be connected with Tags can be queried for as a connection to tags.
Below is an example of asking for a list of Tags, and on each Tag, a list of posts it's connected to.GraphiQL Loading...
The same can be done with categories:GraphiQL Loading...
One key difference between Tags and Categories is that Categories are hierarchical, meaning they can have parents and children.
When querying Categories, by default only the top level categories that are not children of another term are returned.
The children of those categories can be queried like so:GraphiQL Loading...
Sometimes, it makes sense to query for all categories regardless of their parent.
This can be done like so:GraphiQL Loading...
Categories and Tags can also be created, updated and deleted via GraphQL mutations.
PostObjects can also be associated with Categories and Tags.
This is an example of creating a tag:GraphiQL Loading...
This is an example of creating a category:GraphiQL Loading...
In order to update a Tag, you must pass the ID.
This is an example of updating a tag:GraphiQL Loading...
In order to update a Category, you must pass the ID.
This is an example of updating a category:GraphiQL Loading...
In order to delete a Tag, you must pass the ID.
This is an example of deleting a tag:GraphiQL Loading...
In order to delete a Category, you must pass the ID.
This is an example of deleting a category:GraphiQL Loading...
Add Tag to Post
This is an example of a mutation to update a Post and add a tag to it.
Here we pass the ID of the Post we want to update, and an array of Tag nodes we want to append to the Post. We can use tag names or tag IDs to attach to posts.
You can also attach terms in the