1. Home
  2. Docs
  3. Getting Started
  4. Custom Post Types & Taxonomies

Custom Post Types & Taxonomies

Adding custom Post Types and Taxonomies to the WPGraphQL Schema takes just a few lines of code. 

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

Registering 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',
   ] );
} );

Filtering 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 (the one you’re looking at) by it’s URI.

This example also showcases some features of GraphQL queries such as variables, arguments, operation type and operation name.

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). 

This example also showcases some features of GraphQL queries such as fragments, arguments, operation type and operation name.

Get Documents Authored by a Specific User

This example shows a query for a specific user, and documents authored by that user. 

Custom Taxonomies

Exposing Custom Taxonomies to GraphQL requires just 3 fields to be added to the Taxonomy 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 new Taxonomy

This is an example of registering a new “doc_tag” Taxonomy to be connected to the docs Custom Post Type and enabling GraphQL support.

add_action('init', function() {
  register_taxonomy( 'doc_tag', 'docs', [
    'show_in_graphql' => true,
    'graphql_single_name' => 'DocumentTag',
    'graphql_plural_name' => 'DocumentTags',
  ]);
});

Filtering existing Taxonomies

If you want to expose a Taxonomy that you don’t control the registration for, such as a taxonomy registered in a third-party plugin, you can filter the Taxonomy registration like so:

add_filter( 'register_taxonomy_args', function( $args, $taxonomy ) {

	if ( 'doc_tag' === $taxonomy ) {
		$args['show_in_graphql'] = true;
		$args['graphql_single_name'] = 'DocumentTag';
		$args['graphql_plural_name'] = 'DocumentTags';
	}

	return $args;

}, 10, 2 );

Querying Custom Taxonomies

With just a few lines of code, our custom Taxonomy 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.

Query a single Document Tag

This example shows how to query a single Document Tag by its ID.

Query a DocumentTags on a Document

Since the doc_tags Custom Taxonomy has been registered in connection to the docs Post Type, our Schema automatically exposes a connection between Document nodes and DocumentTags.

Tags
Was this article helpful to you? Yes 1 No

How can we help?