Filter array within properties

  • I have a perch collection, each item in the collection has a field allowing related precast to be associated. This fields content is stored as an array:

    The id of the field is accessories and it contain the following array:

    1. Array
    2. (
    3. [0] => anti-flood-airbricks-testsku
    4. [1] => composite-door-2-comp2
    5. )

    I need to filter the collections to show case studies only matching items within that accessories array, this doesn't work:

    1. <?php
    2. perch_collection('Case Studies', [
    3. 'filter' => 'accessories',
    4. 'match' => 'contains',
    5. 'value' => 'anti-flood-airbricks-testsku',
    6. ]);
    7. ?>

    I'm sure I did this before, but cannot remember where? Can someone help me do this again please?

  • Hi Tony,

    To filter by a related item's field you use accessories.field_id. And to filter against multiple values, you can use 'match' => 'in':


    1. perch_collection('Case Studies', [
    2. 'filter' => 'accessories.slug',
    3. 'match' => 'in',
    4. 'value' => 'some-slug,another-slug,look-a-unicorn',
    5. ]);


  • That's not working for me? I think the issue is that the accessories id contains an unnamed array. Hence there is no 'slug' for the filter to match to?

    Here is the showall from the case study:

    url_0 /shop/products/domestic/building-flood-protection/anti-flood-airbricks-testsku
    url_category domestic
    url_1 domestic
    url_subCategory building-flood-protection
    url_2 building-flood-protection
    url_product anti-flood-airbricks-testsku
    url_3 anti-flood-airbricks-testsku
    perch_page_path /shop/products
    breadCrumb DomesticBuilding Flood Protection{...}
    pageTitle Anti-Flood Airbricks (Silicone) - Domestic
    brandID 2
    brandImage {...}
    siteRoot https://dev.m3floodtech.loc
    siteName M3 Global Flood Technologies Ltd
    ogImage https://dev.m3floodtech.loc/as…ec-global-logo-1-w768.jpg
    variantList Available options: Terracotta Price:{...}
    _id 1
    title Case Study Title 1
    _title Case Study Title 1
    slug case-study-title
    1. Array
    2. ( [0] => 10
    3. )
    1. Array
    2. ( [0] => Array ( [columns] => oneColumn [alignment] => left [blockHead] => [textBlock] => Array ( [_flang] => html [raw] => This is a test
    3. [processed] => This is a test
    4. )
    5. [_block_type] => textOnly [_block_id] => qaiz14 [_block_index] => 0 )
    6. )
    1. Array
    2. ( [0] => anti-flood-airbricks-testsku [1] => composite-door-2-comp2
    3. )
    _sortvalue 1000
    perch_item_first true
    perch_item_zero_index 0
    perch_item_index 1
    perch_item_rev_index 3
    perch_item_rev_zero_index 2
    perch_item_count 3
    perch_namespace perch:content

    I'm trying to match within the accessories id which appears to be a simple array.

  • I'm probably not being 100% clear on what I'm trying to achieve. Here's my situation:

    I have a collection of case studies, the case studies template has a content field called "accessories", the user can add items related to that field from the store. The SKUs of the products are stored in the id as an array.

    What I'm trying to do reverse the model and show the case studies on the product page. I have the SKU for the product and I'm trying to filter out the case studies where the SKU is in the accessories id array. Hence show relevant case studies on products pages.

    It's bound to be something simple I'm missing, but I've trawled the documentation and I can't find anything sowing how to filter when the values are stored in an array.

  • I see. When I wrote that field type I didn't think about it being used this way, but it makes sense.

    For now you need to filter by field_id_{index} which is not ideal unless you already know how many products you linked (e.g. if you use the max attribute to limit the number of products that can be selected):

    I opened an issue on GitHub as I think this can be improved.