Alphabetical Glossary Query Loop in Bricks

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

Get access to all 557 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

This Pro tutorial provides the steps for setting up a cloneable Meta Box group query inside a CPT query in Bricks. Scenario: CPT: course Meta…
Categories:
Pro
Horizontal Scrolling Tabs in Bricks

Horizontal Scrolling Tabs in Bricks

Nestable Tabs element in Bricks wraps into multiple lines out of the box at smaller viewport widths. This Pro tutorial provides the steps to remove…
Categories:
Tags:
Pro
Events grouped by months within years based on event date custom field value

Events grouped by months within years based on event date custom field value

How to output events grouped by years and months using three nested query loops in Bricks.
Categories:
Adding any Custom WP_Query loop to Bricks’ Query Loop

Adding any Custom WP_Query loop to Bricks’ Query Loop

Sometimes we may need to create a custom post loop using Bricks' query loop feature, but wish to use our own WP_Query code for more…
Pro
Upcoming Events Grouped by ‘Month Year’ in Bricks

Upcoming Events Grouped by ‘Month Year’ in Bricks

A custom query loop type for showing future events based on a date custom field using three nested query loops.