Custom Post Types

Exposing Custom Post Types to GraphQL requires just 3 fields to be added to the Post Type registration.

  • show_in_graphql: true or false
  • graphql_single_name: camel case string with no punctuation or spaces
  • graphql_plural_name: camel case string with no punctuation or spaces

Register a new Post Type

This is an example of registering a new “docs” post_type and enabling GraphQL support.

add_action( 'init', function() {
   register_post_type( 'docs', [
      'show_in_graphql' => true,
      'hierarchical' => true,
      'graphql_single_name' => 'Document',
      'graphql_plural_name' => 'Documents',
   ] );
} );

Filter Existing Post Types

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:

add_filter( 'register_post_type_args', function( $args, $post_type ) {

    if ( 'docs' === $post_type ) {
        $args['show_in_graphql'] = true;
        $args['graphql_single_name'] = 'Document';
        $args['graphql_plural_name'] = 'Documents';
    }

    return $args;

}, 10, 2 );

Querying Custom Post Types

With just a few lines of code, our custom Post Type is added to the Schema in various places.

Below are some examples, but it’s probably worth exploring the Schema a bit to see where all your new Type shows up in the Schema

Single Document Query

This example shows how to query a single document by it’s URI.

FragmentsThis example also showcases somefeatures of GraphQL queries such as fragments, arguments, operation type and operation name.

query GetDocument {
  documentBy(uri: "docs/getting-started/custom-post-types-taxonomies/") {
    title(format: RAW)
    link
    slug
  }
}

Get Collection of Documents and their Children

This example shows how to query a collection of documents, and their child documents (available for hierarchical Post Types).

FragmentsThis example also showcases somefeatures of GraphQL queries such as fragments, arguments, operation type and operation name.

query GetDocuments {
  documents {
    edges {
      node {
        ...DocumentFields
        childDocuments {
          edges {
            node {
              ...DocumentFields
            }
          }
        }
      }
    }
  }
}

fragment DocumentFields on Document {
  title(format:RAW)
  slug
  link
  content
}