Query by date
This page shows multiple ways to query documents based on a Date field or by a document's first or last publication date.
When querying by a Date field, you will need to use the where argument as shown in these examples.
To query for an exact date, you just need to specify the date you are looking for with the following format: YYYY-MM-DD. The example below shows how to query all the article documents with the "release_date" field equal to January 22, 2017.
query{
allArticles(where:{release_date:"2017-01-22"}){
edges{
node{
title
release_date
}
}
}
}
To query after a given date, you just need to append the field API ID with _after.
Here is an example of a query for all the articles whose "release_date" field is after January 22, 2017.
query{
allArticles(where:{release_date_after:"2017-01-22"}){
edges{
node{
title
release_date
}
}
}
}
To query before a given date, you just need to append the field API ID with _before.
Here is an example of a query for all the articles whose "release_date" field is before January 22, 2017.
query{
allArticles(where:{release_date_before:"2017-07-22"}){
edges{
node{
title
release_date
}
}
}
}
When querying by a Timestamp field, you will need to use the where argument as shown in these examples.
To query after a given date and time, you just need to append the Timestamp field API ID with _after.
Here is an example of a query for all the events whose "event_date" field is after January 22, 2017 at 5pm GMT.
query{
allEvents(where:{event_date_after:"2017-01-22T17:00:00+0000"}){
edges{
node{
title
event_date
}
}
}
}
To query before a given date and time, you just need to append the Timestamp field API ID with _before.
Here is an example of a query for all the events whose "event_date" field is before January 22, 2017 at 5pm GMT.
query{
allEvents(where:{event_date_before:"2017-01-22T17:00:00+0000"}){
edges{
node{
title
event_date
}
}
}
}
There are specific query arguments provided to query by the first or last publication dates.
You can use the firstPublicationDate_after and lastPublicationDate_after arguments to query after a given date and time.
Here is an example that shows how to query all "article" documents with the first publication date after January 22, 2017 at 8AM GMT.
query{
allArticles(firstPublicationDate_after:"2017-01-22T08:00:00Z"){
edges{
node{
title
_meta{
firstPublicationDate
}
}
}
}
}
Here is an example that shows how to query all "blog_post" documents with the last publication date after January 22, 2017 at 8AM GMT.
query{
allBlog_posts(lastPublicationDate_after:"2017-01-22T08:00:00Z"){
edges{
node{
title
_meta{
lastPublicationDate
}
}
}
}
}
You can use the firstPublicationDate_before and lastPublicationDate_before arguments to query before a given date and time.
Here is an example that shows how to query all "page" documents with the first publication date before January 22, 2017 at 8AM GMT.
query{
allPages(firstPublicationDate_before:"2017-01-22T08:00:00Z"){
edges{
node{
title
_meta{
firstPublicationDate
}
}
}
}
}
Here is an example that shows how to query all "event" documents with the last publication date before January 22, 2017 at 8AM GMT.
query{
allEvents(lastPublicationDate_before:"2017-01-22T08:00:00Z"){
edges{
node{
title
_meta{
lastPublicationDate
}
}
}
}
}