Learn about how to interact with Pages using WPGraphQL
Pages are a core WordPress Post Type and are core to the WPGraphQL Schema. On this page, we'll take a look at various ways to interact with Pages using WPGraphQL.
WPGraphQL comes with RootQuery fields built-in to query Page data. You can query for lists of pages, or an individual page. Below, we'll look at some common examples for querying for Pages.
In this example, we query a list of Page nodes. By default, WPGraphQL will return 10 items.GraphiQL Loading...
Pages are a Hierarchical Post Type in WordPress, which means they can have parents and children.
By default, querying for pages returns just the top level pages, and you can get the children by using
childPages field, like so:
There are other fields for hierarchical post types, such as
In many cases, exactly 10 pages won't be what you need for your application. So we can use arguments
pages field to declare how many items we want.
Here we're using the
first argument to declare that we want the first 5 pages.
You can adjust the number of items as well. Here we'll ask for just 1 Page:GraphiQL Loading...
Page connections can be further filtered by using
The args available are largely similar to what's available (and end up mapping to) to the underlying WP_Query. We'll look at a few quick examples, but it's probably best for you to spend some time exploring the Schema Docs to see what options are available.
Also, keep in mind that the entire WPGraphQL Schema is filterable should you need to extend the Schema and provide custom arguments for your applications needs.
- Here's an example of using the
whereargument to get a page with a specific title
When querying a list of nodes (Posts), it's common to need to paginate to fetch more items. The Schema exposes some helpful fields we can ask for to know if there are more items to fetch.
Fields that are helpful for knowing if there are more items to paginate:
- pageInfo.hasNextPage: Whether there are more items going forward
- pageInfo.hasPreviousPage: Whether there are more items going backward
- pageInfo.endCursor: The cursor for the last item in the list
- pageInfo.startCursor: The cursor for the first item in the list
The arguments used for pagination are:
- first: The number of items to fetch. To be used along with
afterfor forward pagination.
- after: The cursor to reference where to fetch from. To be used along with
firstfor forward pagination.
- last: The number of items to fetch. To be used along with
beforefor backward pagination.
- before: The cursor to reference where to fetch from. To be used along with
lastfor backward pagination.
Here is an example of asking for
pageInfo fields to see if there are more items in the list of
You can see that we ask for the field
pageInfo, and on that we ask for
hasNextPage field will be either
false, and the
endCursor will be an opaque
string that acts as a reference to the last node returned in the query.
The cursor can then be used as an argument in a follow-up query, to ask for pages
after that point
in the dataset.
We can use the
first argument to declare how many items we want from the "front" of the dataset.
In this case, we're telling WPGraphQL we want the first 5 most recent pages.
In the response, we see that
hasNextPage responds with
true, meaning there are more pages, and
we can use the
endCursor to ask for the pages
after the last item in this list.
after argument is to be used in conjunction with the
first argument for pagination purposes.
When querying for a list of pages, it's common to need to paginate. When we ask for the first
pages, there's a good chance we'll want to follow-up and ask for the first
10 pages after the
final post in that list.
We can use the
last argument to declare how many items we want from the "back" of the dataset. In
this case, we're telling WPGraphQL we want the last 5 (oldest) pages.
At the bottom of the list will be the oldest item in the dataset. The top item in the list will be the newest of the 5 displayed (because we're going backward here).GraphiQL Loading...
So, we can see that
hasPreviousPage is set to true, which means we have more items
We can take the
startCursor and use that as the value of our next query as our
Here, we take the
startCursor from our previous query, and use it as the value for our
So, what we're saying is that we want
5 more items before the
startCursor in our previous query.
As long as
hasPreviousPage returns true, that means we have more data that we can paginate through.
If we know the ID of a Page, we can query for the page individually like so:GraphiQL Loading...
Sometimes you need to query an individual Page, but you don't have the ID.
idType argument, we can get individual nodes with alternative unique identifiers.
Here's an example of getting a page by it's URI.GraphiQL Loading...
Here's an example of getting a page by it's database ID.GraphiQL Loading...
In this section, we'll look at various ways to mutate Pages.
This is an example of a mutation to create a Page.
There are two required input fields to create a page,
Here you can see that we define our input variable to be of the type
By defining our input in this way, it allows for flexibility for the input variables. Whatever we
input will validate against the shape of the
CreatePageInput! Type in the Schema. This means any
nonNull input fields will need to be required, but any of the other fields are optional,
but if included, still need to be of the specified Type defined in the Schema.
Below is an example mutation to update a page. For this, we must know the ID of the Post.GraphiQL Loading...
Below is an example mutation to delete a page. For this, we must know the ID of the Page.GraphiQL Loading...