Fetching all articles with relation

Hi,
I'm working on a Next.js project where articles have relations to other documents with type "tag" (basically a custom tagging system). I'm trying to build a page where all articles that have a relation to a "tag document" with a certain UID are listed.
I've tried to follow other threads, but nothing seems to work.

This is an example of an article structure, with tags beeing the custom tagging

{
  "id": "ZVekVxIAACIAjvEh",
  "uid": "new-incentives-from-...",
  "url": null,
  "type": "article",
  "href": "https://...",
  "tags": [],
  "first_publication_date": "2023-11-20T12:19:57+0000",
  "last_publication_date": "2023-12-11T10:05:18+0000",
  "slugs": [],
  "linked_documents": [],
  "lang": "en-gb",
  "alternate_languages": [],
  "data": {
    "data": "2022-02-07",
    "title": "New incentives from...",
    "old_content": ['...'],
    "authors": ['...'],
    "tags": [
      {
        "tag": {
          "id": "ZVd6cBIAACAAjiQ2",
          "type": "tag",
          "tags": [],
          "lang": "en-gb",
          "slug": "medical-devices",
          "first_publication_date": "2023-11-20T12:18:12+0000",
          "last_publication_date": "2023-11-20T12:18:12+0000",
          "uid": "medical-devices",
          "data": {
            "uid": "medical-devices",
            "category": "",
            "tag": [
              { "type": "paragraph", "text": "Medical Devices", "spans": [] }
            ]
          },
          "link_type": "Document",
          "isBroken": false
        }
      },
      {
        "tag": {
          "id": "ZVd6mxIAACEAjiV_",
          "type": "tag",
          "tags": [],
          "lang": "en-gb",
          "slug": "pharmacies",
          "first_publication_date": "2023-11-20T12:18:24+0000",
          "last_publication_date": "2023-11-20T12:18:24+0000",
          "uid": "pharmacies",
          "data": {
            "uid": "pharmacies",
            "category": "",
            "tag": [{ "type": "paragraph", "text": "Pharmacies", "spans": [] }]
          },
          "link_type": "Document",
          "isBroken": false
        }
      }
    ],
    "slices": ['...'],
    "meta_description": null,
    "meta_image": {},
    "meta_title": null
  }
}

I've tried with this approach, but i'm not able to use "filter"

 const test = await client.get({
      predicates: [
        predicate.at("document.tags.tag.uid", params.uid),
        predicate.at("document.type", "article"),
      ],
      orderings: {
        field: "document.order",
        direction: "asc",
      },
    });

Does anyone have any feedback?

Hi @mattbreda. There are a few things that need to be updated with your query to get this to work.

  1. You can't query by a linked document using its UID. In order for this to work, you'll first need to query the tag by its UID to retrieve its ID.

  2. Then you can use the ID to get your articles.

  3. To sort the results by a field in your article type, you'll need to use the syntax shown in the example below: field: "my.article.order". To order by a meta field such as last_publication_date, then you would use "document": field: "document.last_publication_date".

Here's what this would all look like:

const tag = await client.getByUID("tag", params.uid);

const test = await client.get({
      predicates: [
        predicate.at("my.article.tags.tag", tag.id),
      ],
      orderings: {
        field: "my.article.order",
        direction: "asc",
      },
    });