WordPress was originally designed as a platform to write blog articles, but over the years, ambitious developers have found new and creative ways to store content in WordPress. Today, we're going to look at a few plugins that can be used to customize the WordPress schema, and we're going to update our Gridsome site to pull in these the new data.

Installing the Plugins

Before we can start extending the Schema, we'll need to install the following plugins. The first two are in the WordPress Plugin repo, but the second two must be installed via a manual upload.

Once these are all installed, we can start building a custom post type.

Extending the Schema

We already have post types for Posts & Pages, but lets say we're going to start creating a training course to sell on our site. To enable this functionality on our main website, we'll create a custom post type with the following schema;

  • Description - Rich text block
  • Price - Number
  • IsInPreview - True/False
  • ReleaseDate - Date Picker
  • Author - Text

Lets get started by heading over to CPT UI > Add/Edit Post Types, populate the basic settings, then scroll to the bottom to setup the GraphQL settings. The other fields can be ignored. Once you are done, click Add Post Type to save it.

https://cdn.brianmorrison.me/media/2020/5a6e9847-bbe3-48b9-a91d-b60c73621755

https://cdn.brianmorrison.me/media/2020/efea4f3f-c270-4d38-97e9-5da5c14dd61c

https://cdn.brianmorrison.me/media/2020/f5d9483e-f4f8-4b13-a561-da8742e5a3e6

On the left nav, you should now have a new entry for Courses.

https://cdn.brianmorrison.me/media/2020/b2b81297-7041-412c-b85a-96234e9f7b55

Now lets add those other fields we need. Head into Custom Fields then Add New. Lets name our group Course Fields. We can use the built in editor for the courses description, but the other fields will need to be added here. Click on Add Field to start adding them. You'll also need to update the Rules in Location to be Course instead of Post. The last thing to do before saving is scrolling down to the bottom and setting the GraphQL settings.

https://cdn.brianmorrison.me/media/2020/6860d7c7-1082-4f5f-b469-5456a7f6837e

https://cdn.brianmorrison.me/media/2020/81abdc7e-b45e-43a8-b330-36c732d2db94

Now lets add a course. Why not Building Websites with WordPress and Gridsome?! Make sure you add some text in the description and populate all the fields we just setup.

Pulling into Gridsome

Now that we have the data setup, lets fire up Gridsome and head to the explore URL to make sure we can get the data from the GraphQL layer. Execute the following query and you should see the new post type along with the custom fields we setup.

{
  courses {
    edges {
      node {
        id
        title
        content
        courseFields {
          isInPreview
          author
          releaseDate
          price
        }
      }
    }
  }
}

https://cdn.brianmorrison.me/media/2020/6381bc48-adbc-4b02-a885-b48459bc8801

There you go! You now have a system that can be extended to fit pretty much any data structure you need, all using the WordPress publishing tools you might already be familiar with. The next step would be to setup another template just like we did in the previous article to display the data.

Happy Coding!