Forum

Thread tagged as: Question, Problem

Filtering category based on dropdown selection

Hi there,

I'm trying to filter categories based on the value of a dropdown. If user select yes in the dropdown, the category will be shown on the home page. My template looks like this:

<perch:before><ul class="column column--by-four"></perch:before>
    <li class="column__item">
        <perch:category id="catTitle" type="smarttext" label="Title" required="true" />
        <perch:category id="catSlug" type="slug" for="catTitle" suppress="true" />
        <perch:category id="home_visible" type="select" label="Visible on home page" options="Yes, No" allowempty="false" required="true" />
    </li>
<perch:after></ul></perch:after>

I output the categories like so:

perch_categories(array(
      'set' => 'clients',
      'template' => 'clients.html',
      'filter' => 'home_visible',
      'match' => 'eq',
      'value' => 'Yes'
));

Somehow, I get no result or a empty array when I skip-template. If I remove the filter, match and value, here's the output I get:

Array
(
    [0] => Array
        (
            [home_visible] => Yes
            [catID] => 4
            [setID] => 2
            [catParentID] => 0
            [catTitle] => Something
            [catSlug] => something
            [catPath] => clients/something/
            [catDisplayPath] => Something
            [catOrder] => 1
            [catTreePosition] => 002-001
            [catDepth] => 1
            [...]
        )

Any idea? Weird part, if I filter based on catTitle, it does work.

Cheers,

Robin Pick

Robin Pick 5 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm, interesting!

Could you add debug to the page and let me know what it outputs?

Ha, looks like the debug has something interesting: Invalid query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'home_visible' in 'where clause'

If that helps: the same issue occurred when I tried to use a checkbox.

Debug Message
SELECT * FROM perch2_pages WHERE pagePath='/index.php' LIMIT 1
Using template: /templates/pages/attributes/seo.html
Using template: /templates/pages/attributes/seo.html
Using template: /templates/pages/attributes/seo.html
Using template: /templates/pages/attributes/seo.html
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/index.php' OR regionPage='*' ORDER BY regionPage DESC
SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Our work' AND (regionPage='/our-work.php' OR regionPage='*')
SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=6 AND idx.itemRev=75)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 12
Using template: /templates/content/our-work/work_listing.html
SELECT * FROM perch2_categories ORDER BY catTreePosition ASC
SELECT setID FROM perch2_category_sets WHERE setSlug='clients' LIMIT 1
SELECT main.* FROM perch2_categories main WHERE 1=1 AND (home_visible='Yes') AND setID='2' ORDER BY catTreePosition ASC
Invalid query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'home_visible' in 'where clause'
Using template: /templates/categories/clients.html
SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1
SELECT main.* FROM perch2_categories main WHERE 1=1 ORDER BY catTreePosition ASC
Using template: /templates/categories/category.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think it may be that categories can't be filtered on a custom field. I think we were trying to optimise by not building a search index for those. If it's coming up as a requirement we'll revisit that.

It's not a big deal but I do believe that could be useful in some cases – though it doesn't seem that many people asked for this. In the end, though it isn't ideal, I still can use css to hide the category.

Thanks for your answer!

Quick follow-up on this, maybe it will be useful for some: I found a better way to show/hide specific categories on a given page. Thanks to Perch's flexibility, that works great.

Instead of using a dropdown in the category template (that would essentially set the visibility of the category item to show or hide using a class), I now use a region with the category tag display-as="checkboxes".

<perch:categories id="clients" label="Clients" set="clients" required="true" display-as="checkboxes" divider-before="Client to be shown on the home page">
      <perch:template path="categories/clients.html" />
</perch:categories>

What I like about it is that I can simply include the template of the category in my region template and that I don't have to use css to display: none/block the category item. For the content manager, it makes also more sense to decide which client to show on the home page from a region rather than in the category section of the CMS.

Anyway, I'm again super pleased by how flexible Perch is. Great job!

Cheers.

I so appreciate the follow-up. Thank you!