1

I'm trying to retrieve entries related to the current entry, based on a tag field called topics. Simple stuff.

Now, I use Craft's native GraphQL implementation so I'm bound to the possibilities this interface gives me. In twig, it would be quite easily solved as described in the following topic: Search for related entries based on tags

It seems like the relatedTo property of the entries() query only supports a single ID, without the ability to scope my results based on a certain field. I would expect to be able to do something like the following:

entries(
    section: "article",
    limit: 2,
    relatedTo: {
        sourceElement: 123, // ID of the current entry
        field: 'topics'
    }
) {
    id,
    title
}

This just gives me the following error: Expected type [Int], found {sourceElement: 123, field: \"topics\"}.

Now, if I resort to the documentation it indeed describes that only an entry's ID can be used as the value of relatedTo, like this:

query QueryName {
  entries(
      section: "article",
      limit: 2,
      relatedTo: 123 // Current article ID
  ) {
      id,
      title
  }
}

This does work (it doesn't error), but results in an empty entries array. Even though there's entries that have the same topics (tags) assigned to them.

I'm kinda lost on how to actually retrieve related entries. Should I just revert to using the search param and feed it a concatenated list of the current post's tags?

Any elegant solutions would be greatly appreciated!

Thanks in advance!

1 Answer 1

1

I might be missing something here but wouldn't passing the current entry's tag IDs rather than the entry ID work for you?

query QueryName {
  entries(
      section: "article",
      limit: 2,
      relatedTo: [123,456] // Current article's tag IDs?
  ) {
      id,
      title
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.