StanLindsey Posted February 25, 2019 Share Posted February 25, 2019 I'm working on an e-commerce shop. Currently, a categories default sorting is by descending price. The client doesn't have images for all products, wants the category sorted by products with images by default. Easy: sort=-image This sorts by the filename, essentially alphabetically. I'd like to sort by those with images, with a secondary sort of price descending. sort=-image, sort=price This does not work, because the image sort is sorting alphabetically, not by the existence of the field. So you end up with this: name | image | price product1 | product1.jpg | £12.34 product2 | product2.jpg | £43.43 product3 | product3.jpg | £12.20 product4 | product4.jpg | £76.20 product5 | product5.jpg | £12.50 product6 | null | £18.50 product7 | null | £16.21 product8 | null | £13.10 Notice that images prices are essentially ordered randomly, those with null image are ordered descending. So I need to have the selector sort by the existence of the image field, not by the content of the image field. I need to do this as part of the selector because some of these categories have 2000+ products and I cannot do it in-memory. Thoughts? Link to comment Share on other sites More sharing options...
BitPoet Posted February 25, 2019 Share Posted February 25, 2019 Have you tried sorting by -image.count? 5 1 Link to comment Share on other sites More sharing options...
StanLindsey Posted February 25, 2019 Author Share Posted February 25, 2019 Perfect - solved. I didn't think of using the sub-selectors in the sort param for some reason. 1 Link to comment Share on other sites More sharing options...
szabesz Posted February 26, 2019 Share Posted February 26, 2019 This reminds me that we have a recent discussion of "how to put count into good use": https://weekly.pw/issue/248/ : "exact match queries using Page reference fields" 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now