Forum
Problem filtering collection on a category
Hi,
I'm trying to filter a collection with a category. I've made a reduced test case to illustrate the issue I'm having.
I have a collection called Test Facts that uses the the following test-facts.html
template:
<perch:content id="title" type="text" size="l" label="Title" title="true" />
<perch:categories id="locations" label="Locations" set="locations" display-as="checkboxes">
I then have another collection called Test Restaurants that uses the the following test-restaurant.html
template that allows me to select an item from the Facts collection as related content:
<perch:content id="title" type="text" size="l" label="Title" title="true" />
<perch:related id="test_facts" collection="Test Facts" label="Facts" max="1" required="true">
<perch:categories id="locations" label="Locations" set="locations" display-as="checkboxes">
</perch:related>
I have a page template which calls the collection using the following hardcoded category filter (I have made sure that I have items that are assigned this category):
perch_collection('Test Restaurants', [
'template' => 'collections/_test-listing.html',
'category' => 'locations/london/',
]);
My _test-listing.html
template is as follows:
<perch:before><ul></perch:before>
<li>
<perch:content id="title" type="text" />
<perch:related id="test_facts" collection="Test Facts">
<perch:categories id="locations" set="locations">
<perch:before><ul></perch:before>
<li><perch:category id="catTitle" /></li>
<perch:after></ul></perch:after>
</perch:categories>
</perch:related>
</li>
<perch:after></ul></perch:after>
<perch:noresults>
<p>No results</p>
</perch:noresults>
When I visit a page that calls the collection and filters on the hardcoded category, I'm returned <perch:noresults>
even though there are items present in the collection that have this category assigned to them via the relationship with the Facts collection.
Is it possible to filter by category like this (i.e. a category assigned via a relationship)? Is there anything that I'm missing in my templates? Is it a scoping issue?
Thanks in advance for any help.
The
category
filter is filtering Restaurants, but the categories are applied to Facts.The Facts categories are probably indexed as part of the relationship, but you'll need to use a regular
filter
to access them, not thecategory
filter.Hi Drew,
Great, thanks, that sounds promising.
I've modified my
collection
call as follows, using a filter as suggested, but I'm still not getting output.The above looks correct to me but maybe I'm missing something. Is this what you were thinking? Can you suggest anything?
Many thanks.
That's what I was theorising, but it may not be correct. If it's not working then it's likely not possible to do.
That's a shame, I thought I had a perfect multilingual set up there!
Is this something that you think might be looked at?
My principal use case is for building out multilingual sites and using collections and relationships to separate language independent content (the facts) from language specific content (the descriptions). This can keep my templates really clean and works well for editors. And being able to also filter by categories as described above would fit this model really well.
If there's anything you can suggest that might achieve what I'm looking for, any advice would be appreciated.
Thanks for your help.
In the past when i have had trouble using category filtering I just transferred the category to a multiple choice field in the collection. The only limit is that editors cannot create 'categories'. Or you could look at a bit of relational database theory and using a third collection ( not sure of I properly understand your relational logic ) as a join or lookup filter. I imagine the first solution might offer a 'slug' which can filter both collections on a single page if the choices are identical.