Grouped Query List for Bricks
A nestable Bricks element that puts a heading above each group of query loop results, such as a month, a category, a year or a letter. The grouping is done on the server, so the page is complete HTML with no script.
Version: 0.4.0 · Requires: WordPress 6.0+, PHP 8.1+, Bricks · See the live demo
What it does
- Groups the results of one query loop by month, year, any date format, taxonomy term, author, first letter of the title, or a custom field.
- Shows a heading for each group, with an optional count and optional jump links.
- Puts the heading above the items or beside them, and can keep the heading in view while its items scroll past.
- Can make each group collapsible, with all, the first, or none open.
- Lays the items out in as many columns as fit, by the width of the space the list sits in.
Installation
- Upload and activate the plugin. Bricks must be active.
- In the Bricks editor search for Grouped Query List (Layout group).
Quick start
- Add the element. It arrives with one block that has a query loop on it.
- Keep the class
gl-itemon the block that carries the loop. The element uses it to find the items. - Choose what to group by.
- Order the query by the same thing you group by.
Order the query to match
The element groups results in the order the query gives them. Group by month and the query should be ordered by date. Group by letter and it should be ordered by title. If the order does not match, the same group can appear twice. Turn on Merge groups that are not next to each other to join them, which puts each group where it first appears.
Group by
- Date — a PHP date format, default
F Y - Year
- Term — the first term of a taxonomy you choose
- Author
- First letter — ignores the, a and an; anything that is not a letter goes under #
- Custom field — a field name, with a date format if the field holds a date. Dates stored as
Ymd(the format Advanced Custom Fields uses) are understood. - Attribute — group by a data-gl-group attribute you set yourself on the item, for anything else
Several levels
Under Then group each of those by you can add up to two inner levels. Each level has its own group by, date format, taxonomy, field, part and heading tag. Examples:
- Events by year, then by month: group by Post date (year), then add an inner level of Post date with the format
F. - A directory by category, then by first letter.
- Posts by the top-level term, then by the child term.
The query should be ordered so that the outer level comes first, such as by date for years and months. With the join option on, each outer group still appears once, and the inner groups are joined inside it. Jump links list the top level only. Inner headings use the next smaller tag by default.
Dynamic data and ACF repeaters
Choose Dynamic data under Group by and type any dynamic data tag, such as {acf_event_date:Y}. The tag is read for each item, so it works in any loop: posts, terms, users, or the rows of an ACF repeater or Meta Box group. For a repeater, set the loop type on the item block to the repeater, and group by one of its sub-fields. Use a date tag with a format to group dates, for example Y for the year and F for the month name.
Fill in Sort key with a second tag that gives a sortable value, such as the same date with the format Ymd. With Sort the groups on, groups then follow the real date or number and not the alphabet. Without a sort key, groups come in the order the rows are stored, or sort by heading.
Sorting
By default groups come in the order the query gives them. Set Sort the groups on any level to put them in order whatever the query does:
- Ascending — A to Z, 1 to 9, oldest first
- Descending — Z to A, 9 to 1, newest first
- Most items first
Date groups are sorted by the real date, not by their text, so months come in calendar order and not alphabetically, and years sort correctly. A group with no value, shown under the fallback heading, always goes last. Sort the items inside each group orders the items by date or by title.
Part of a value
For title, term, author, post type, parent and custom field you can use only part of the value:
- The first characters — for example the first 4 characters of a reference code
- The text before a separator — for example the part of a title before a dash
- The text after a separator
- The first word
For a taxonomy you can also choose Its top-level parent, which groups by the highest level of a hierarchy, such as a country instead of a town. Dates are controlled by the date format instead, so year, month, weekday or any other part is a format code. An item with several terms is grouped under the first one.
Settings
- Heading tag, Show counts, Jump links
- Heading position — above or beside; Heading width when beside
- Sticky headings and the distance from the top
- Collapsible groups and which start open
- Minimum item width for the columns, Gap and Item gap
- Fallback label for items with no value
- Colours for heading, line, count and jump links
Tips
- A heading beside the items moves above them when the space is narrower than the width you set, so it works in a sidebar.
- Use normal pagination. Load more buttons and infinite scroll add items after the groups, outside them.
- The items get a plain default look, which is easy to override with your own styles.
Known limits
- The element groups only the items its query loop returns. A loop set to 20 posts spreads those 20 posts across the headings, and a group cannot page or load more on its own. For a separate Load more button or page numbers in each group, use Nested Query for Bricks.
- Only one query loop per element.
- Items that load after the page has loaded are not grouped.