Query by tags

Prismic no longer recommends the gatsby-source-prismic-graphql plugin

With recent changes to Gatsby, Prismic no longer recommends the gatsby-source-prismic-graphql plugin that this documentation uses. Read more about the future of Prismic and Gatsby. We highly recommend using the gatsby-source-prismic instead. The documentation for this plugin can be found in the plugin's github README.

We will leave this documentation here for now, but will change it in the future when we determine the best approach for Prismic & Gatsby.

This page gives you an explanation and examples of how to query documents by their tags. You can do this by using the tags or tags_in arguments as shown below.

Query by a single tag

In this example we will query all documents of the type Page with the tag "tutorial". Then we retrieve a Rich Text field that has the API ID of "video_name" and the meta data with the tags:

Copy
query PagesByTagQuery {
  prismic {
    allPages(tags: "tutorial") {
      edges {
        node {
          video_name
          _meta {
            tags
          }
        }
      }
    }
  }
}

Query by multiple tags

When querying by multiple tags, there are two options. One where you return the documents that have all of the tags and another where you return the documents that include any of the tags.

Documents that include all tags

In order to query documents that include all the tags specified, use the tags argument with an array of strings.

For example, here is a query that will retrieve all the documents of the type Page that contain the tag "science" and the tag "tutorial". Then we retrieve a Rich Text field that has the API ID of "video_name" and the meta data with the tags:

Copy
query PagesByTagsQuery {
  prismic {
    allPages(tags: ["science ", "tutorial"]) {
      edges {
        node {
          video_name
          _meta {
            tags
          }
        }
      }
    }
  }
}

Documents that include any tags

In order to query documents that include any the tags specified, use the tags_in argument with an array of strings.

For example, here is a query that will retrieve all the documents of the type Page that contain the tag "Science" or the tag "Entertainment". Then we retrieve a Rich Text field that has the API ID of "video_name" and the meta data with the tags:

Copy
query PagesBySomeTags {
  prismic {
    allPages(tags_in: ["Science", "Entertainment"]) {
      edges {
        node {
          video_name
          _meta {
            tags
          }
        }
      }
    }
  }
}