Count this artist's products that are in order pages

I have a series of artists (own page), artists have their products (own page), these products may be sold and if they are sold they appear in a structure item in an order (own page).

Now, given an artist I need to get all their products that have been sold.

The way I am doing it is, basically:

- get all the products of an artist
- if all their products > 0
    - foreach their products
        - foreach sitewide orders
            - foreach structure item within order
                - if item == this product
                    - count +1 for this product

…but as each artist have several produts, and there are many orders, and many items in each order, with so many nested loops this process takes quite a long time.

What would be a more efficient approach in this case ?

Thank you

Get all structure items into a single collection and filter this collection by artist. It might make sense to store a log of sold products of artists in a database (e.g. SQLite), if the collection gets huge.

Thanks for the answer… but you mean just replacing the third nested loop with a filter on the structure item as a collection ? Any other suggestion on the rest of the pseudo-code ?

Thanks again

In general I’d start with the orders, not loop through the products.

  • check if artist has products
  • fetch all orders into a collection
  • filter that collection
1 Like

Ok, got it, thank you

Do you know if there is a way I can filter pages by an item’s field in a structure in the pages ?

So, to cleanly filter orders by if there is, in their ‘product list’ structure item, and item with ‘id’ == ‘blah’.


Yes, that is possible, you can use pluck to get an array of all the values of a specific field inside the structure.