Custom Post Types
In order to use Custom Post Types with WPGraphQL, you must configure the Post Type to
show_in_graphql using the following fields:
- show_in_graphql (boolean): true or false
- graphql_single_name (string): camel case string with no punctuation or spaces. Needs to start with a letter (not a number). Important to be different than the plural name.
- graphql_plural_name (string): camel case string with no punctuation or spaces. Needs to start with a letter (not a number). Important to be different than the single name.
This is an example of registering a new “docs” post_type and enabling GraphQL Support.
If you want to expose a Post Type that you don’t control the registration for, such as a post type registered in a third-party plugin, you can filter the Post Type registration like so:
Custom Post Type UI is a popular WordPress plugin that enables users to register Custom Post Types and Custom Taxonomies from the WordPress dashboard via a user interface. The WPGraphQL for Custom Post Type UI plugin provides fields to Custom Post Type UI that allow you to set whether the Post Type or Taxonomy should show in GraphQL, and set the GraphQL Single Name and GraphQL Plural Name.
Querying content in a Custom Post type is similar to querying Posts and Pages. The difference being that the
graphql_plural_name will be shown in the Schema.
So, assuming you registered the post type as shown above, with the
docs, you would be able to query like so:
And if your
Doc, you would be able to query a single Doc like so:
All post types have the
ContentNode Interface applied to their GraphQL Type.
WPGraphQL exposes fields that a post type has registered support for using the post_type_supports, and leaves out fields that a post type does not support.
Supported fields are applied to the GraphQL Type using Interfaces.
An example, would be the title field.
If your Custom Post Type supports the
title field, the GraphQL Type representing your post type will have the
NodeWithTitle Interface applied to it.
Mutating Custom Post types is pretty similar to mutating Posts & Pages.