Pages field query by structure

Hi all,

I am struggling since almost a day to filter a pages field list by a value I have given the pages by a Structure Field.
A little bit more details:

I have a list of locations. Therefore I have created a template blueprint called locations. Inside this blueprint I can do two things:

  1. Creating new locations using the pages-section, so every locations gets its own page.
  2. Creating categories such as castle, garden, hall, gym, … (you get the idea) and also creating a unique id for every category like 123456 using a Structure field

Inside the created location page I can choose the categories by using a multiselect field querying the created categories of the parent site called locations.

So far so good. Now the real question:
On other pages of my site, I want to show a list, where some selected locations are shown. On one page, there should it be possible to choose only locations of one specific category.
Inside the blueprint of this page, I have used the pages-field. When I query the locations with

query: site.find('locations').children

I will get indeed a list of all created locations. But in this case I want to get a list of only one category, which I created on the locations site in the Structure field.
When I use

query: site.find('locations').children.filterBy('categoryid', '==', '123456')

I just get the error: Your query must return a set of pages.

I just don’t get it what I am querying wrong… maybe you have an idea how I get the prefiltered set of pages?

Thanks, Nick

P.S. Is it possible to sort the pages inside the pages field, not by number but alphabetically

Since you are querying from a multiselect, you have to pass the separator as fourth parameter:

query: site.find('locations').children.filterBy('categoryid', '==', '123456', ',')

Cool, thank you. Works perfectly.

@texnixe one other question on this topic:
On the locations page I am using the pages section as described in my initial question. Now I want to display the categories inside the cardlets as an info text.

Therefore I use the following expression:

info: {{ page.parent.categories.toStructure.filterBy('categoryid', 'in', page.category.split(','), ',')

With this line I get the ids of the categories inside the Structure field, so that on a cardlet is the info text e.g. 2<br />5. Sadly I am not aware of what I am doing wrong to get the title of the element of the structure field, so that 2 = garden and 5 = apartment, …

When appending the .toJson to the above line, I receive the following JSON-Object:

{"2":{"categoryid":"123456","id":"2","slug":"garden","title":"Garden"}}

but on some pages I get the following:

[{"categoryid":"123456","id":"2","slug":"garden","title":"Garden"}]

I hope that this is still a bloody beginners mistake and you can help me.

Thanks,
Nick