27th Feb '24
/
3 comments

Alphabetical Glossary Query Loop in Bricks

Alphabetical Glossary Query Loop in Bricks

Updated on 30 Jun 2025

This Pro tutorial provides the steps to group posts (can be of any post type) by their starting letter in Bricks.

We shall define a custom function that loops through the posts and returns an array with the unique (among the ones that posts start with) alphabets as the keys and the corresponding posts' array as the values.

The values are associative arrays having ID and title keys. Titles are needed so the posts can be arranged in ascending order.

We shall then create a custom query type called "Alphabetical Glossary" and feed the keys of the array returned by our custom function. This query type will be used to output the unique alphabets.

For showing the posts for each alphabet, we use the PHP query editor and do a look up of the matching post IDs via our custom function.

Nested queries are used in the list view and for the tab contents in the tabs view.

This is a classic example that demonstrates the power of querying in Bricks, PHP loops and arrays and some outside the box thinking esp. for the tabs view where we use the Tabs (Nestable) element.

Step 1

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 626 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Alternating Posts from Two Post Types in Bricks

Alternating Posts from Two Post Types in Bricks

How to output posts from two seperate post types, alternating in a query loop.
Categories:
Pro
Nav Menus Custom Query Types in Bricks

Nav Menus Custom Query Types in Bricks

Updated on 21 Aug 2024 This Pro tutorial shows how custom query types for each navigation menu can be generated in Bricks. This enables us…
Pro
Posts Grouped by Post Dates in Bricks

Posts Grouped by Post Dates in Bricks

Setting up query loops for “Post Dates” with an the inner loop displaying all posts published on that day.
Categories:
Pro
Related Posts by Taxonomy in Bricks

Related Posts by Taxonomy in Bricks

In the previous Posts Related by Current Post's Terms in Bricks tutorial, we showed how posts related to the current single post that have the…
Categories:
Bricks Query Loop – Posts Authored by Logged in User

Bricks Query Loop – Posts Authored by Logged in User

How to display only the posts authored by the currently logged-in user in a query loop.
Categories:
Pro
Post Views Counter Query Loop in Bricks

Post Views Counter Query Loop in Bricks

How we can output the most viewed posts in your Bricks site in a query loop.
Categories: