11th Aug '23
/
4 comments

Sort Posts by Meta Box Group Sub Field in Bricks

This Pro tutorial provides the steps to order posts in Bricks by the value of a Number-type of sub field that is inside a Meta Box group.

Use case: Sort posts by year.

If the year field were not inside a group, it is straight forward to order by the field value:

But if the field is inside a group like this:

there is no built-in way, that I know of, to set up ordering using the controls that Bricks provides in the editor.

What Bricks does provide however, is a set of excellent filter hooks for developers to modify most aspects of the output programmatically.

We shall use the bricks/query/result filter to intercept the query result by

  • getting the array of post objects in the result
  • getting the array of post IDs
  • sorting the above array using a custom comparison function
  • getting the array of post objects from the above sorted array of post IDs
  • setting the posts property of the result object to the above array of sorted post objects

to arrange the posts in the order of those that have the sub field populated in either ascending or descending order followed by the rest in the default date descending order.

Sample data from our test site:

Event 0:

Event 1: 2026

Event 2: 2023

Event 3: 2022

Event 7: 2040

(rest of the event entries have the field empty)

On the front end before implementing the tutorial:

On the front end after implementing the tutorial - with posts arranged by the Event Year sub field in descending order:

Step 1

Inside a Section's Container, add a Posts element or set up a query loop on a Block element.

Configure the query and set

Order by: Meta numeric value

If you are not editing a template that applies to the event CPT archive, set Post type to Events.

Add the following in child theme's functions.php 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 630 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Update Post Meta From Another Custom Field’s Value on Post Publish/Update when using Meta Box

Update Post Meta From Another Custom Field’s Value on Post Publish/Update when using Meta Box

In the Meta Box Facebook group a user asks: Hi all, wondering if I can get some help. For custom field 1, I am using…
Categories:
Tags:
Pro
MB Favorite Posts in Bricks

MB Favorite Posts in Bricks

This tutorial explores using MB Favorite Posts in Bricks, including outputting the list of favorites via a query loop.
Categories:
Pro
Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

This Pro tutorial provides the steps to filter the rows of a Meta Box cloneable group to only those that have a date custom field…
Random Meta Box Image

Random Meta Box Image

Displaying a random image from a set of images from a Meta Box Images Advanced-type field.
Categories:
Tags:
Pro
Meta Box Date Field Value Custom Format in Bricks

Meta Box Date Field Value Custom Format in Bricks

Updated on 3 Apr 2024 Note: A custom function is no longer needed. Use the date format filter, as mentioned in the Bricks documentation. Ex.:…
Categories:
Tags:
Pro
Meta Box Related Project’s Featured Image URL on Single Reviews

Meta Box Related Project’s Featured Image URL on Single Reviews

Updated on 26 Jun 2025 In a recent website I worked on, a requirement was to get the URL of the featured image of a…