Filter perch_collection by attribute of type perch:category

  • I have an object with a category price. When a user creates a new instance of this object, it's only allowed to select one value of this category (only one price). When I want to filter my list of objects by price I get “No Results” because the attribute price of the object is an array of length 1 and if I apply a function to each one of the objects of my list mapping the array of prices of length one to an integer it is not used in sorting. It's like sorting is done before this mapping so it cannot sort a list of objects by an attribute of array type.

    This is my code:

    1. perch_collection('Objects', [
    2.     'each' => function($item) {
    3.         $item["price"] = $item['price'][0];
    4.         return $item;
    5.     },
    6.     'template' => $listing_template,
    7.     'sort' => 'price',
    8.     'sort-order' => 'ASC',
    9.     'sort-type' => 'numeric',
    10. ]);
  • drewm

    Approved the thread.
  • The sort options are added to the SQL query, whilst the 'each' function is applied to the items returned from the query, so this will not effect the data used to sort. Can you provide the Object collection template so we can see what fields are being used?