Forum
Filtering a list by a field value
I have a list of parks that are managed within a Perch Runway collection. One of the fields for each park is "region" and on each park page I'm trying to output a list of parks within the same region as the current page. I see that it's possible to create a form to filter on multiple fields, but that is more complex than what I'm trying to do.
I though I could pass the "region" value as a variable and then call that variable, but it doesn't seem to be working. Has anyone done this, or can you point me to some documentation?
Thanks, Tim
It sounds like a good fit for categories - have you used those before?
When your park page loads, it will get all the info for that park - including which category/region it's in.
Pass that category id into another query which calls all the parks in that category which are not equal to the current park's id.
Quasi-code:
That should return the other parks in that category/region which you can then list.
Since this is Runway, https://docs.grabaperch.com/runway/relationships/ might also be a good fit.
Categories seems like a promising solution, but I can't figure out how to make it work. I've set up the categories for the various regions.
My collection is called "Preserves" and my category id is "preserveregion". How can I get the region category for the current page and then list other Preserves with the same region?
I have a list of all preserves showing with:
Any help would sure be appreciated.
The thing that I don't understand in this method is how I pull the current park id. Thanks for any guidance you can give.
If you use
skip-template
along withreturn-html
you can get both the HTML output and the fields (like your park ID) when querying the collection.I'm already pulling in all of the data into the page from the template using
skip-template
andreturn-html
and then using them as variables. Here's the top of the page where I'm pulling in the fields:Then further down the page I'm trying to display two lists: 1 using related content from an Events collection, which is working fine, and 1 listing preserves (parks) in the region of the one they are visiting. Here's that code--and I know the second bit isn't working properly, as it's not filtering using the current preserves "preserveregion".
Been knocking my head against this one for a few days, and as more of a designer than coder, I've hit the wall.
Thanks for any help. Tim
So you want to filter where the slug is not equal to the current item. I can't tell from your code what you're trying to do but it's going to be something like:
Close. I also have four regions that each preserve is in. The region is stored in a category titled "preserveregion" (and also a field titled "region" from my first try.)
Each preserve has it's own page and I'm trying to list other preserves in the same region. Seems like it should be fairly easy, but it's not. My current code shows all preserves, without filtering:
You apply the filter from my post previously:
Thanks so much--that worked!!!
Ack! It actually didn't work--I'm still getting all preserves in the list, not just that of the current page.
Is
'cat'
being passed in through the URL for the current preserve (fromperch_get('cat')
)?If not, at the top of the page, get the category of the current preserve from your
skip-template
query and pass that into the'category' =>
option.The
'cat'
isn't in the URL, since these are links to collection pages. I'm pulling in the category at the top of the page and declaring it as a variable. The first category displays as below when I addperch:showall
:The code block pulling in the list:
With this I get no list at all.
perch_get()
is used to get values that have been appended to a URL. If you have no category string appended to your URL, then replaceperch_get('cat')
with whatever the variable you have declared is called.Thanks so much for helping me with this one. Thanks to you I finally have it working!
This is what ended up being the working code.