Contentful logo

Contentful Community

Order entries by field on relationship entry

I’m making a site that has a product listing. Each product has a brand (a One Reference) and brands have a priority (a Number) field. I want to sort products by their brands’ priority.

Using the JS SDK I’ve tried specifying the order query parameter as fields.brand.priority and fields.brand.fields.priority, but get the following error:

{
  "sys": {
    "type": "Error",
    "id": "InvalidQuery"
  },
  "message": "The query you sent was invalid. Probably a filter or ordering specification is not applicable to the type of a field.",
  "details": {
    "errors": [
      {
        "name": "unknown",
        "path": [
          "fields",
          "brand",
          "en-US",
          "fields", // (when specified)
          "priority"
        ],
        "details": "The path \"fields.brand.en-US.fields.priority\" is not recognized"
      }
    ]
  },
  "requestId": "7eb500c6-07dc-4d8b-aacc-ece710205c7b"
}

Is it possible to sort on relationships’ fields?

I asked support which replied that this is not possible at the moment.

Hey, have you considered using search on reference? in this way you can get all the brand of a single priority, it could useful for parsing the data in your application