Filter perch_categories by multiple categories?

  • Hi,


    I'd like to be able to specify which categories I'd like to be output by perch_categories, I've used a comma delimited string , but only the first category is shown?


    PHP
    1. perch_categories([
    2. 'set' => 'content-type',
    3. 'template' => 'menu.html',
    4. 'filter' => 'catPath',
    5. 'match' => 'eq',
    6. 'value' => 'content-type/case-studies/','content-type/publications/','content-type/resources/',
    7. ]);


    I've tried passing an array into the 'value' like this:


    PHP
    1. perch_categories([
    2. 'set' => 'content-type',
    3. 'template' => 'menu.html',
    4. 'filter' => 'catPath',
    5. 'match' => 'eq',
    6. 'value' => ['content-type/case-studies/', 'content-type/publications/', 'content-type/resources/'],
    7. ]);


    But I get an error in perch:debug: expects parameter 1 to be string, array given


    Anyone know if this is possible? :/

  • I think its because you are using 'match' => 'eq', which is doesn't work with multiple items. you can try the following,


    Code
    1. perch_categories([
    2.     'set' => 'content-type',
    3.     'template' => 'menu',
    4.     'filter' => 'catSlug',
    5.     'match' => 'in',
    6.     'value' => ['case-studies', 'publications', 'resources'],
    7. ]);

    I prefer using slug, as you've already passed the set in. If it still expects a string the following should work

    Code
    1. perch_categories([
    2. 'set' => 'content-type',
    3. 'template' => 'menu',
    4. 'filter' => 'catSlug',
    5. 'match' => 'in',
    6. 'value' => 'case-studies, publications, resources',
    7. ]);
  • Thanks andrewmman / Byron Fitzgerald


    Combining your suggestions did the trick!


    I stuck with catPath as I wanted to replace the hardcoded paths with an array that would contain the category paths.


    PHP
    1. perch_categories([
    2. 'set' => 'content-type',
    3. 'template' => 'category.html',
    4. 'filter' => 'catPath',
    5. 'match' => 'in',
    6. 'value' => 'content-type/case-studies/,content-type/publications/,content-type/resources/',
    7. ]);


    Further up in the template I created $selected_cats, which allows the admin to choose what categories to output on a page.


    PHP
    1. perch_categories([
    2. 'set' => 'content-type',
    3. 'template' => 'category.html',
    4. 'filter' => 'catPath',
    5. 'match' => 'in',
    6. 'value' => $selected_cats,
    7. ]);


    I used $selected_cats as the value.