Skip to main content
6 votes
Accepted

Group entries by section

You can use the Twig group filter. {% set entries = allowedSearchableEntries.search(query).order('score').limit(100) %} {% set entriesBySection = entries|group('section') %} {% for section, ...
Aaron Berkowitz's user avatar
5 votes
Accepted

Grouping entries by category - most efficient approach

One improvement I think could be made is to fetch only the ID's of your hotTubs entries at the start of your code. You're only using this to get the related categories and fetching ID's is faster than ...
Tom De Smet's user avatar
  • 1,456
3 votes
Accepted

How to group assets by category/sub-category

I’m assuming you want to do this in Twig rather than PHP, since you haven’t specified nor have you provided any code. First, fetch all assets, eager-loading the categories. {% set assets = entry....
Ben Croker's user avatar
  • 7,796
3 votes
Accepted

Passing a variable to an include

You set the variable that your are passing in to your include to a string. And in your include you try to access it like an object. The variable which you’re passing in to your include is set to a ...
carlcs's user avatar
  • 36.3k
3 votes
Accepted

Sort entries in a category via entry field

You are getting this error because the "entries" fields are always considered to be a query (ElementCriteriaModel) that can return more than one element. Therefore, if you know for sure that you will ...
kant312's user avatar
  • 496
2 votes
Accepted

Better way to group by category and include full category entry

Your actual code is pretty effective – the problem is that it's potentially going to result in a lot of queries. This is a good example of the so-called N+1 query problem, where each iteration over ...
Mats Mikkel Rummelhoff's user avatar
2 votes

Group entries by section, then order and combine sections

You could do this in a few steps (warning: this is untested code): 1) Define an array of the sections you cited, in the correct order: {# Define a list of the sections you want to display in the ...
kant312's user avatar
  • 496
2 votes
Accepted

Set OrderBy on Grouped Query Involving Smart Map Field

Okay, I've been able to solve this and figured I'd share what I discovered. The key for each group is the value of the state so really what I was after here was a key sort and just didn't realize it. ...
Tim Knight's user avatar
  • 1,022
2 votes

Order grouped entries

Stumbled upon this looking to answer similar problem. You should be able to do this now without a plugin, using the multisort filter where you iterate through your profiles, e.g.: {% for profile in ...
littlered's user avatar
  • 151
2 votes
Accepted

Archive Not Returning Correct Date Variable

Your postDate.day will output the number of the day in the month so if your entries postdate is 02.dd.yyyy your output is 2. Your array index will be something like [2, 3, 5, 12, 31] thus these values ...
Robin Schambach's user avatar
2 votes
Accepted

Grouping a large number of entries

First, based on your example code I don't think you actually need to loop on the entries – you could simply group them on the brand name, and then loop on the keys for the grouped array (the keys ...
Mats Mikkel Rummelhoff's user avatar
2 votes
Accepted

Change order within grouped archive

Assuming the years are currently rendered in the correct (descending) order, an easy way to make sure the entries are rendered in ascending order would be to simply reverse the entriesInYear array, i....
Mats Mikkel Rummelhoff's user avatar
2 votes

Grouping by Date (Day) Not Working Correctly

Figured it out. I actually asked for all entries for the group, rather than just the entries (events) that matched the date. {% set passEvents = craft.entries.section("event").eventForwardPass("Yes")...
nonprofit_tech's user avatar
2 votes

Order entries by related "Year" category and paginate?

You could sort using the multisort filter (docs) then paginate using Yii's ArrayDataProvider - see this post for reference. {# Fetch your entries and order by theYear descending #} {% set ...
Oli's user avatar
  • 7,515
2 votes
Accepted

Stuck when grouping entries to get any other parameters like url, id etc

The group filter creates an associative array so the location Twig var in your loops is the array item key, which is a string. I think you'll have to get the location object that's related to the ...
Mark Northrop's user avatar
2 votes
Accepted

Grouping entries by a custom date conditional

You could fetch your entries then split them in three arrays which would give you the ability to then add headings manually and display as you need to. {# Define today and three arrays to hold your ...
Oli's user avatar
  • 7,515
2 votes

Group by related entries field

A little late to the party here, but in case anyone else comes across this post. The suggestion that August was making here was very close, but when I tried something similar, I was running into the ...
stevecomrie's user avatar
2 votes
Accepted

Group entries by custom fields

Assuming you can't change your data model easily to use relationships pointing to a 2-level Structure for State > Suburb (which would be a more robust approach), you can use the group filter to ...
James Smith's user avatar
  • 5,689
1 vote

Group entries by custom fields

Wasn't that hard actually, just needed to stop and think about it for a bit. {% set buildings = craft.entries.authorId(currentUser.id) %} {% set states = [] %} {% set suburbs = [] %} {% for building ...
Tyssen's user avatar
  • 665
1 vote

Group/sort by multiple criteria

You can pass multiple columns to the orderBy parameter like so: {% set people = craft.users().groupId(1).orderBy('isLeadership DESC, isActive DESC, lastname ASC').all() %} {% for person in people %} ...
Jalen Davenport's user avatar
  • 3,045
1 vote
Accepted

Group by related entries field

I think you're nearly there! Give this a shot: {% set allEntriesByPrename = allEntries | group('{{ (object.composerName | first).lastName | upper | slice(0, 1) }}') %} I believe the issue is that ...
August Miller's user avatar
1 vote

Order grouped results by dropdown order

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, ...
billythekid's user avatar
1 vote

List elements grouped by multiple dates

You could loop through all your events, and create a date-keyed hash with the ids of the entries - assuming your (e.g {% set saleDays = {} %} {% for entry in events %} {% for row in ...
Nick's user avatar
  • 146
1 vote
Accepted

Grouping by select - keep order of options

The group filter loops through the passed elements in the order that they are returned by your query and builds the groups array in the order that it encounters the values being grouped. So there’s no ...
Steve Rowling's user avatar
1 vote

Select query question

Why don't you use the prepared ElementCriteriaModel in the category and change the query? {% for category in craft.categories.group('whatever') %} {% set articles = category....
Robin Schambach's user avatar
1 vote
Accepted

error when trying to loop through grouped entries according to dropdown field

You have to remove the entry from your entry.resourceType {% set allEntriesByCat = allEntries|group('resourceType') %} will work
Robin Schambach's user avatar
1 vote

Using the Group filter with Squarebit Charge plugin

I got this to work with the following: {% set allCharges = craft.charge.charges.limit(5) %} {% set charges = allCharges|group("dateCreated|date('F')") %} {% for month, details in charges %} <...
Roi Agneta's user avatar
  • 1,463
1 vote

Search entries ordered by sections

Something like this should get you started, q is the querystring in your results url: {% set query = craft.request.getParam('q') %} {% set entries = craft.entries.search(query).level(1).order('score')...
erwinheiser's user avatar
1 vote

Can I move existing tags, which are currently in a global tag group, to new the new tag groups I've set up?

There is no way to do this natively in Craft through the control panel. You could either write quick plugin that programmatically moved the existing tags to new tag groups or write some custom SQL ...
Brad Bell's user avatar
  • 67.6k
1 vote
Accepted

Get Section Handle From Group

In your example, you're attempting to use a SectionModel instance as the key for your grouped entries. This is not possible. What actually happens behind the scenes is the group filter generates an ...
Stephen Lewis's user avatar

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