Hot answers tagged

8

I assume you're using .search() – a common mistake is to try to combine orderBy('score') with custom fields, which doesn't work because the score attribute isn't an actual field, but a computed value that Craft "injects" into the element model. If I understand you correctly, you want to render any "featured" entries at the top of the search results. You ...


5

Close! This should do it: {% set products = craft.commerce.products({ relatedTo: (category), order: 'defaultPrice', }) %} With the quotes it's a string. Without the quotes, Twig is expecting a variable you've defined called defaultPrice that doesn't exist.


4

To get this working I submitted a patch to 'SortByField' - https://github.com/dannynimmo/craftcms-sortbyfield - to get this plugin to work with Commerce products - and I now do this: {% set product_results = craft.commerce.products.search(query).order('score') %} {% set article_results = craft.entries.search(query).order('score') %} {% set ...


4

Not exactly what you're looking for, but it gets you closer. This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's {% paginate %} tag along with your other requirements. IMO, you're probably going to be better off doing this in PHP either using a Template Hook or a custom ...


3

There's this super useful SuperSort plugin that would make this possible. {% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %} {% set sortedOccurrences = allOccurrences|supersort('sortAs', sortAsTemplate) %}


2

Try to group the entries sorted by size using the group filter: {% set entriesSorted = entries|supersort('sortAs', '{ size }', SORT_NUMERIC) %} {% set entriesByBrand = entriesSorted|group('brand.first.title') %} {% set entriesByBrand = entriesByBrand|supersort('ksort') %} {% for brand, entriesInBrand in entriesByBrand %} {% for entry in entriesInBrand %...


2

I did something like this as a workaround: Loop over entries, fetching all data you need and adding it to an array. Sorting that array with SuperSort and than displaying all info by looping over the array. {% set tourDates = [] %} {% set artists = craft.entries.section('artists') %} {% for artist in artists %} {% if artist.tourDates %} {% for ...


2

I recon Supersort is they way to go, as its got some really handy functions to make this sort of thing a breeze. Try with the following: {% set entries = craft.entries.section('presenters').order('RAND()').limit(10) %} {% set sortedEntries = entries | supersort('sortAs', '{lastName}') %} {% for item in sortedEntries %} {{ item.title }} {% endfor %}


2

You can query on multiple category groups like so: craft.categories.group('suitability, finish, shape, material, edge, surface').all() It can also be done with an array: craft.categories.group([ 'suitability', 'finish', 'shape', 'material', 'edge', 'surface' ]).all() Whenever you invoke .all() it returns an array. As those arrays were merged, ...


2

For those of you reading through this post, I thought it might be useful to provide a bit of background on my question. The Craft Docs list "multisort" under Filters and it sounded like something that would work nicely for me. However, the Craft Docs do not give an example, instead directing the reader to Yii docs. Those docs confirmed it was exactly what I ...


1

I think this should do what you're looking for: {% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}


1

I hope I am not completely missing the point, but I guess like that: Initial 5: {% for block in block.eventList.eventDate('>=' ~ (now|atom)).orderBy('eventDate asc').limit(5).all() %} {{ block.eventTitle }} {% endfor %} rest: {% for block in block.eventList.eventDate('>=' ~ (now|atom)).orderBy('eventDate asc').offset(5).all() %} {{ ...


1

I figured this out, with help from a GitHub thread or too. It appeared that using the sort filter on an array of objects was problematic. I was able to fix this by sorting the keys in speakers instead of directly sorting speakers. Working code: {% for speakerKey in speakers|keys|sort %} {% set speaker = speakers[speakerKey] %} <li>{{ speaker....


1

You could wrap this in another for loop and put a condition on the inner fors, something like {% for option in myPositionDropdown %} <h2>{{ option.value }}</h2> {% for position, playersInPosition in playersGrouped if position == option.value %} <ul> {% for player in playersInPosition %} {% set playerData = craft....


1

Try removing the quotes on 2nd/3rd arguments: {% set rides = furniture|multisort(furniture['price'],[SORT_DESC],[SORT_REGULAR]) %}


1

I don't know if there's a quick/easy way to do this within the admin area, but if you go to Entries, then click on your Structure, there's a little 4-dot symbol to the left of the Entry's title (make sure you have Structure selected in the drop-down to the right of the Search bar). You can click that 4-dot icon and move them around manually. Doing this will ...


1

Your second attempt is sound. I don’t really have an idea why it doesn’t work, but I bet it has to do with the SQL that is caused by setting the section param to an array. What I would try next is the following, kind of a merge of your ideas. {% set profileIds = craft.entries.section('profiles').limit(null).ids() %} {% set eventIds = craft.entries.section('...


1

I used the preparse plugin to save a date value from a SuperTable field into a static field as 'yyyymmdd'. I could then sort my entries based on that static field.


1

It's possible to pass MySQL functions to the order parameter. Using the MySQL LENGTH function, I believe the following should do the trick (assuming your "model number" field's handle is modelNumber): {% set entries = craft.entries.section('yourSection').order('LENGTH(modelNumber) asc, modelNumber asc') %}


1

You could do all of this in PHP. Build your criteria models, merge the results and sort them with usort.


Only top voted, non community-wiki answers of a minimum length are eligible