You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Kontent.ai Delivery API按关联项字段排序内容?

解决Kontent.ai Delivery API按关联项字段排序的问题

Kontent.ai的Delivery API不支持直接通过linkedItems[0].elements.xxx这种嵌套语法排序,得通过**投影(Projection)**把关联字段拉平到顶层,再基于投影后的字段排序。

具体操作步骤:

  • 先用project操作将关联的Page Template里的publication_date字段提取出来,给它设置一个顶层别名(比如template_pub_date)
  • 再用这个别名执行排序逻辑

以你使用的SDK为例,代码可以修改为:

var response = await client.GetItemsAsync<WebPage>(
    new EqualsFilter("system.type", "web_page"),
    new ProjectBuilder()
        .AddLinkedItem("page_template", linked => linked
            .AddField("publication_date", "template_pub_date")
        ),
    new OrderByDescending("template_pub_date")
);

如果是直接调用REST API,对应的URL参数格式如下:

https://deliver.kontent.ai/[YOUR_PROJECT_ID]/items?
system.type=web_page&
project=elements.page_template.elements.publication_date:template_pub_date&
order=elements.template_pub_date[desc]

原写法报错的核心原因是:Delivery API的排序逻辑不支持在条件中直接遍历linkedItems数组,必须通过投影把关联字段映射到当前条目顶层,才能被排序规则识别。

另外,由于你每个Web Page都强制关联一个Page Template,所以投影时不会出现空值问题,可以放心使用。

内容的提问来源于stack exchange,提问作者styles0121

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 15:29:58