Forum

Thread tagged as: Question

Is there a way to filter by multiple category sets? [SOLVED]

Hi,

Is there a way to filter by multiple category sets?

Setup

My collection has two category sets,

  1. <perch:categories id="solutions">
  2. <perch:categories id="sectors">

I've got two pages, listing categories from the same collection

Page 1: Solutions

Filtering the solutions category is straightforward. I'm just filtering against the url to see if it contains the name of the category I want to display.

 perch_categories([
    'set' => 'solutions',
    'template'=>'solutions--list.html',  
    'filter' => 'catPath',
    'match' => 'contains',
    'value' => perch_get('solution'),
    ]);

Page 2: Sectors

The sectors category name isn't in the url, however the solutions category is, and the collection item is using both category sets.

 perch_categories([
    'set' => 'sectors',
    'template'=>'sectors--list.html',  
    'filter' => 'catSlug',
    'match' => 'eq',
    'value' => 'transit', // Need this to be dynamic, not hardcoded.
    ]);

Question

Is there a way to combine these two category sets?

So I can filter the sector categories using the solutions category present in the url.


Summary

Perch Runway: 3.1.1, PHP: 7.1.12, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $, with PDO
Server OS: Darwin, cgi-fcgi
Installed apps: content (3.1.1), assets (3.1.1), categories (3.1.1)
App runtimes: <?php $apps_list = [ ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /Users/stephen/Repositories/project/cms/perch
PERCH_CORE: /Users/stephen/Repositories/project/cms/perch/core
PERCH_RESFILEPATH: /Users/stephen/Repositories/project/cms/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
REQUEST_URI: /perch/core/settings/diagnostics/
DOCUMENT_ROOT: /Users/stephen/Repositories/project/cms
HTTP_HOST: project.local
Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago

My guess would be to use a filter array, same as perch_content_custom

COPIED EXAMPLE:

perch_content_custom('Properties', [
    'filter'=> [
        [
            'filter'=>'bedrooms',
            'match'=>'gte',
            'value'=>3
        ],
        [
            'filter'=>'price',
            'match'=>'lte',
            'value'=>500000
        ],
    )
]);
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Stephen,

You can omit the set option (it is optional).

You can filter by category path:

perch_categories([
    'filter' => 'catPath',
    'match' => 'in',
    'value' => 'solutions/catSlug/,sectors/catSlug/'
]);

Or by category slug:

perch_categories([
    'filter' => 'catSlug',
    'match' => 'in',
    'value' => 'some-slug,another-cat-slug'
]);

Hi,

Thanks for the replies.

I managed to get it work with a trusty Callback Function.

I can now filter perch_categories with two category sets.

Here's what I used:

$sectors = perch_categories([
    // Lists ALL categories from category set 'sectors-en'
    'set' => 'sectors-en',
    'template' => 'home--list.html',

    // Check which items in the collection match the filter
    // This filters the items by the first category set, because the category name is in the URL
    'each' => function($item) {
          $item['related_sectors'] = perch_collection('Solutions EN',[
                'category' => $item['catPath'],
                'template' => 'solutions-callback.html', // contains an ID from my main perch_collection template, hidden with CSS.
                'filter' => 0, //Check the URL 
                'match' => 'contains',
                'value' => perch_get('solution') // Matched route: en/[slug:solution]/sectors
                ],true
          );
          return $item;
          },
]);

home--list.html is just a category list template, but it needs to have the key related_sectors in the template

<-- home--list.html -->
 <perch:category id="related_sectors" type="hidden" encode="false">

solutions-callback.htmlis a stripped down version of my master perch_collection template. For some reason it has to contain at least one ID (from the master template) to make this whole thing work.

solutions-callback.html just has this in it:

<-- solutions-callback.html -->
<style display:"none";visibility="hidden"><perch:content id="col_title" type="text"></style>

I'll go back and remove that inline CSS, but at the moment I'm just happy this appears to work.