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 633 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

This Pro tutorial covers how posts of a Bricks query loop can be filtered to show only the ones whose true / false or checkbox…
Categories:
Tags:
Pro
Dynamically Displaying Connected Content Offer Images in Bricks using Meta Box Relationship

Dynamically Displaying Connected Content Offer Images in Bricks using Meta Box Relationship

Consider the following scenario from a recent project I worked on. CPT 1: project CPT 2: content-offer Taxonomy for Projects: remodel-typeSample term names: Backyard Cottages…
Pro
Adding FAQ page Schema with SlimSeo Schema & Meta Box

Adding FAQ page Schema with SlimSeo Schema & Meta Box

SlimSeo Schema is a plugin by the people behind both SlimSEO & Meta Box. It takes care of adding all the page schema across your…
Categories:
Pro
Dynamic Accordion in WordPress using Meta Box and Alpine.js

Dynamic Accordion in WordPress using Meta Box and Alpine.js

This Pro tutorial provides the steps to implement an accordion using Alpine.js in WordPress that pulls the values of sub fields of a Meta Box…
Pro
Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

How to link a Bricks post-creation form on a post to a newly created post of another post type using Meta Box relationship.
Pro
HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

How to set the source of Bricks’ Image Gallery element on a post to images in the selected HappyFiles Folder(s) via a Meta Box Taxonomy…
Output HappyFiles Folders in Metabox Select-Field

Output HappyFiles Folders in Metabox Select-Field

This tutorial is part of a series where I'm gonna show you how to output custom HappyFiles Galleries and Sliders. Part one will cover a…
Categories:
Custom Image Field Avatar in WordPress

Custom Image Field Avatar in WordPress

Learn how to use a Meta Box image field value as the author profile image on single post pages in WordPress/Bricks.
Categories:
Tags: