Forum

Thread tagged as: Question

How do I restrict the output of a collection to a subcategory?

Example

I've created a test category with three subcategories:

  • Colours
    • Pink
    • Pink and Purple
    • Red

Collection code

Here's my collection code:

    perch_collection('faqs', [
        'category'=> 'faqs/' . perch_get('cat') . '/' . perch_get('subcat'),  
        'template' => '_faq/faq--list-category.html',
    ]); 

The code half works as expected.

The problem

The Pink subcategory shows both the contents of Pink plus Pink and Purple

However, the Pink and Purple and red subcategories only show the contents of that subcategory - they work as expected.

Question

How do I restrict the output to just the subcategory selected?

Here's a screencast showing the problem: https://recordit.co/94HAcBOYZg

I thought perhaps I could use a filter, match and value but I can't get them to work. The screenshot below shows the showall from faq--list-category.html used in my collection.

Template Showall

This is the showall from faq--list-category.html

Perch Screenshot

Health check

  • PHP 7.0.22 is up to date
  • MySQL 5.6.35 is up to date
  • Image processing available

Summary information

Perch Runway: 3.0.14, PHP: 7.0.22, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $, with PDO
Server OS: Darwin, cgi-fcgi
Installed apps: content (3.0.14), assets (3.0.14), categories (3.0.14), perch_blog (5.6.1), perch_forms (1.10)
App runtimes: <?php $apps_list = [ 'perch_forms', 'perch_blog', ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /Users/stephen/Repositories/my-site/cms/perch
PERCH_CORE: /Users/stephen/Repositories/my-site/cms/perch/core
PERCH_RESFILEPATH: /Users/stephen/Repositories/my-site/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/my-site/cms
HTTP_HOST: my-site.local
Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

Hi Stephen,

I think if you can turn on debug for the page and take a look at the query that's being generated, you may find that it looks for category names LIKE 'faqs/colours/pink%' - so that will pick up pink-and-purple too.

Maybe try:

'category'=> 'faqs/' . perch_get('cat') . '/' . perch_get('subcat') . '/', 

or call the second category Purple and Pink instead...

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

It's like Duncan says: you need a trailing slash.

If you go to the category listing in the control panel, you'll find the category paths are listed with a trailing slash. So technically set/cat/subcat is not the complete path. set/cat/subcat/ is.

Great, thanks Dunc and Hussein. That worked!