Query by a Content Relationship or Link field

This page shows you how to query your results by a Content Relationship/linked document field. Check these other articles in you're looking to get content from a Group field or get content from a Link field.


❗️It must be a top-level field

When using the Content Relationship or Link field as arguments, they have to be at the top level of a Custom Type and not inside a Slice. It also only works with the document ID and not the UID.

The where argument

The where argument allows you to retrieve a linked document from a Link or Content relationship field. For example, here we're looking for a document with the ID of "XECfdABzACEACaD9" in a Link field with the API ID of example_link:

Copy
where: { example_link: "XECfdABzACEACaD9" }

The where argument can also be used with Key text, Select, and Number fields.

Query by a Content Relationship field example

In the following example you can see that we are querying every post where the Content Relationship field called link contains an id field with the value XECf-BAAACEACaD9. This value is the ID of the linked document.

You must use the document ID

When querying by a Content relationship field, the value passed to the argument can only be the ID of a given document. If you use a different value, for example: a UID field, the query won't work.

Copy
query {
  allPosts(where : {link: "XECf-BAAACEACaD9"}) {
    edges {
      node {
        _meta {
          uid
        }
      }
    }
  }
}

NOTE: If your content relationship field API ID has an underscore, for example linked_field, this can cause the query to break.

Query by a Content Relationship field in a Group example

If your Content Relationship field is inside a group, you just need to specify the Group, then the Content Relationship field. Again the Group field needs to be in the top-level 'Static' section of your document for this to work.

In this example, we are querying every post where the Content Relationship field called link inside the group field called linksgroup contains an id field with the value XYNDLREAACIAjhs1. This value is the ID of the linked document.

Copy
query {
  allPosts(where : {linksgroup: {link: "XYNDLREAACIAjhs1"}}) {
    edges {
      node {
        _meta {
          uid
        }
      }
    }
  }
}

You must use the document ID

When querying by a Content relationship field, the value passed to the argument can only be the ID of a given document. If you use a different value, for example: a UID field, the query won't work.


Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Get in touch with us on our Community Forum.