Forum

Thread tagged as: Question, Runway

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

Timothy Swan

Timothy Swan 0 points

  • 1 week ago
Duncan Revell

Duncan Revell 44 points
Registered Developer

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:

perch_collection('Parks',[
category => 'catPath of selected Park',
filter => 'Park id',
match => 'neq',
value => 'selected Park id',
])

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:

    perch_collection('Preserves',[
        'template' => 'preserves/region_listing.html',
        'sort' => 'title',
                'count'=>'10',
                'sort-order'=>'RAND',
    ]);

Any help would sure be appreciated.

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:

perch_collection('Parks',[
category => 'catPath of selected Park',
filter => 'Park id',
match => 'neq',
value => 'selected Park id',
])

That should return the other parks in that category/region which you can then list.

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.

Drew McLellan

Drew McLellan 2251 points
Perch Support

If you use skip-template along with return-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 and return-html and then using them as variables. Here's the top of the page where I'm pulling in the fields:

    //this pulls in the post to extract the variables
    $preserve = perch_collection('Preserves', [
        'skip-template' => true,
        'return-html'   => true,
        'template'      =>'preserves/preserve.html',
        'filter'        => 'slug',
        'match'         => 'eq',
        'value'         => perch_get('preserve'),
    ]);

  if(!isset($preserve[0])) {
    // do whatever you do if no results
    PerchSystem::redirect('/preserves');
  }
// declare the variables here

    $title = $preserve[0]['title'];
    $description = $preserve[0]['description'];
    $og_description = $preserve[0]['description'];
    $og_image = $preserve[0]['fbimage'];
    $keywords = $preserve[0]['keywords'];
    $og_type = $preserve[0]['og_type'];
    $og_title = $preserve[0]['title'];
    $domain = $preserve[0]['domain'];
    $og_url = $preserve[0]['url'];
    $site_name = $preserve[0]['site_name'];
    $events = $preserve[0]['events'];
    $preserveregion = $preserve[0]['preserveregion'];

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".

  <?php if (perch_get('preserve')){
    //related events from Events collection
    perch_collection('Events',[
        'template' => 'events/preserve_event_listing.html',
        'sort'=>'eventDateTime',
        'sort-order'=>'ASC',
        'filter'=>array(
            array(
                'filter'=>'category_names',
                'match'=>'neq',
                'value' => 'hidden'
            ),
            array(
                'filter'=>'eventDateTime',
                'match'=>'gte',
                'value' => date('Y-m-d 23:59:59')
            ),
            array(
                'filter' => 'preserve.slug',
                'value' => perch_get('preserve'),
                'match' => 'eq',
            ),
        )
    ]);
}
?>
<h2>Other MCHT preserves</h2>
<?php 
    perch_collection('Preserves',[
        'template' => 'preserves/region_listing.html',
        'sort' => 'title',
        'count'=>'10',
        'sort-order'=>'RAND',
        'category' => perch_get('cat'),
    ]);
?>

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

Drew McLellan

Drew McLellan 2251 points
Perch Support

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:

perch_collection('Preserves', [ 
'filter' => 'slug', 
'match' => 'neq', 
'value' => perch_get('preserve'), 
]);

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:

<?php 
perch_collection('Preserves',[ 
'template' => 'preserves/region_listing.html', 
'sort' => 'title', 
'count'=>'10', 
'sort-order'=>'RAND', 
'category' => perch_get('cat'), ]); 
?>
Duncan Revell

Duncan Revell 44 points
Registered Developer

You apply the filter from my post previously:

perch_collection('Preserves',[  
'template' => 'preserves/region_listing.html',  
'sort' => 'title',  
'count'=>'10',  
'sort-order'=>'RAND',  
'category' => perch_get('cat'), 
'filter' => 'slug', 
'match' => 'neq', 
'value' => perch_get('preserve'),
]); 

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.

Duncan Revell

Duncan Revell 44 points
Registered Developer

Is 'cat' being passed in through the URL for the current preserve (from perch_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 add perch:showall:

Array
(
    [0] => 1
)

The code block pulling in the list:

<?php 
    perch_collection('Preserves',[
    'template' => 'preserves/region_listing.html',
    'sort' => 'title',
    'count'=>'10',
    'sort-order'=>'RAND',
    'category' => perch_get('preserveregion'), //also tried 'category' => $preserveregion
    'filter' => '$slug',
    'match' => 'neq',
    'value' => perch_get('preserve'), 
  ]);
?>

With this I get no list at all.

Duncan Revell

Duncan Revell 44 points
Registered Developer

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 replace perch_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.

<?php 
    perch_collection('Preserves',[
    'template' => 'preserves/region_listing.html',
    'sort' => 'title',
    'count'=>'10',
    'sort-order'=>'RAND',
    'category' => $preserveregion,
    'filter' => 'slug',
    'match' => 'neq',
    'value' => perch_get('preserve'), 
  ]);
?>