Custom Posts

There are two ways to fetch CPT data, depending on the CPT having been mapped to the schema or not.

1. CPTs mapped to the schema permalink

For CPTs which have been mapped to the schema (eg: this plugin provides types Post and Page, to represent CPTs "post" and "page" respectively), we use fields customPost and customPosts.

In this case, the query can be resolved using the corresponding type to that CPT. Because the results can include entities from different types (such as Post and Page), these fields return the CustomPostUnion type.

Custom Post Union
Custom Post Union

When fetching results from a union type, we need to specify the fields to retrieve through fragments. These can be evaluated on interface IsCustomPost, which is implemented by all CPT types, or on each individual type, such as Post or Page.

Enabling CPT types permalink

By default, only type Post is added to the CustomPostUnion type. To add more types, please follow the instructions in guide Adding a custom post type to the schema.

Fetching custom posts permalink

When querying field customPosts, we indicate the CPTs via field argument customPostTypes, which receives a list of strings, with the CPT names as defined in WordPress (such as "post", "page", etc).

In the query below, we fetch custom posts with CPTs "post" and "page". We display their fields through 3 fragments, which evaluate if the entity implements IsCustomPost, or is of type Post or Page:

2. CPTs not mapped to the schema permalink

When a CPT has not been mapped to the schema yet (eg: some CPT "portfolio" from a custom plugin), we use fields genericCustomPost and genericCustomPosts.

Because their types do not exist, the query will be resolved using a type called GenericCustomPost, which contains all the common properties to CPTs (title, content, excerpt, date, etc).

Generic Custom Post
Generic Custom Post

Then, we can retrieve their fields directly, without using a fragment.

Fetching custom CPT data permalink

Using GenericCustomPost, we can only request those fields which are common to all CPTs; fetching custom data from some CPT is not supported (such as fetching the price data for a custom CPT "product").

To fetch custom CPT data, instead, we need to map the CPT to the schema (as shown earlier on):

  • Create a type Product
  • Attach a price field to it
  • Fetch it via customPosts (or a new Root.products field)

Supporting custom CPTs permalink

The CPTs which can be retrieved when executing field genericCustomPosts must be configured in the Settings. By default, only entities with CPT post are retrieved.

To support more CPTs, please follow the instructions in guide Adding a custom post type to the schema.

Fetching generic custom posts permalink

When querying field genericCustomPosts, we indicate the CPTs via field argument customPostTypes, which receives a list of strings, with the CPT names as defined in WordPress (such as "post", "page", etc).

In the query below, we fetch 5 custom posts with CPT "post":

Share on 🐦 Twitter | πŸ‘ŽπŸΎ Facebook