Alphabetical Glossary Query Loop in Bricks

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

Instant access to 390+ Bricks code tutorials with BricksLabs Pro