kentico-kontentkontent-ai

Order Kontent.ai items by linkedItem element field


I'm struggling to order items returned via the Kontent.ai delivery rest API using a field on the linked item of that piece of content.

My content model is as follows:

Each Web Page must have a Page Template as a linked item, and I ideally want to order all web pages on the linked page template posted date on list pages for example. But I can't seem to find anywhere in the docs to suggest this is possible.

Has anyone had any success with doing this?

Here is what my current orderByDescending call looks like:

orderByDescending("elements.page_template.linkedItems[0].elements.publication_date")

But this gives me an error.


Solution

  • Unfortunately, It's not possible to order or filter based on values nested in linked items at the API level and by extension SDKs as the filtering methods are just abstractions of the API filtering operations. This has to do with the structure of the API response:

    {
    "items": [],
    "modular_content": {},
    "pagination": {}
    }
    

    In your case, the resulting items array contains the items of Web page type, which merely hold a codename reference to their respective Template items. All the actual linked items are stored in the modular_content array, which is unordered.

    The only way to achieve this is to retrieve all required items from API and subsequently perform the ordering in your code.