Select related pages with KQL

Hello,

I am using the KQL plugin and I want to get all products as well as the related categories.

I am sending the following query:

const res = await axios.post("/query",
        {
            query: "site",
            select: {
              title: "site.title",
              url: "site.url",
              products: {
                query: "page('products').children.listed",
                select: {
                  id: true,
                  title: true,
                  category: {
                    query: "site.category",
                    select: {
                      headline: "category.headline",
                    }
                  },
                }
              },
            }
          }
        );

Now I want to rerieve all products including the headline of the related category of that product, but I am getting null for the category headline: category: {headline: null}

categories

This is my category.yml file:

# Each page blueprint must have a title, the title may be different from the file name
title: Category

# Each page can have an icon that is shown in page listings when no preview image is available.
icon: 🖼


status:
  draft: true
  listed: true

columns:
  - width: 2/3
    # This columns only has a single field
    fields:
      headline:
        label: Headline
        type: text

And this is my product.yml file:

# Each page blueprint must have a title, the title may be different from the file name
title: Product

# Each page can have an icon that is shown in page listings when no preview image is available.
icon: 🖼

status:
  draft: true
  listed: true

columns:
  - width: 2/3
    # This columns only has a single field
    fields:
      text:
        type: blocks
      sent: fields/visitors
      category:
        label: Category
        type: pages
        query: site.find('categories')
        multiple: false
  - width: 1/3
    # This second column holds a fields section with multiple fields
    # More about fields sections: https://getkirby.com/docs/reference/panel/sections/fields
    sections:
      meta:
        type: fields
        fields:
          # If you need the same field in multiple locations, you can define them once and reuse whereever needed.
          # Here we use a files field defined in `/site/blueprints/field/cover.yml`
          cover: fields/cover
      files:
        type: files
        template: blocks/image

What am I doing wrong?

EDIT -----------------------------:

I managed it now that the category ID that I have set in the panel is now correctly returned with this query:

  {
            query: "site",
            select: {
              title: "site.title",
              url: "site.url",
              products: {
                query: "page('products').children.listed",
                select: {
                  id: true,
                  title: true,
                  category: true
                }
              },
            }
          }

category-works

But now I need to know how to actually get the category headline within the product query.

I found the solution here: How to create "related pages" field, where related pages would be selected from other page children? - #3 by Roman

Apparently I have to call the method toPages on the related page.

Full working query:

{
            query: "site",
            select: {
              title: "site.title",
              url: "site.url",
              products: {
                query: "page('products').children.listed",
                select: {
                  id: true,
                  title: true,
                  category: {
                      query: "page.category.toPages",
                      select: {
                          id: true,
                          headline: true,
                      }
                  }
                }
              },
            }
          }
1 Like