9th Jan '24
/
0 comments

Ordering Meta Box group rows by a sub field in Bricks

In the BricksLabs Facebook group a user asks:

Any idea how to set the sort order of a cloneable group in the query? In this case, I just need to sort social media accounts alphabetically. I know it can be done manually, but that isn't a viable option with multiple people entering content.

In the past, we showed how posts could be sorted/ordered by a Meta Box group field's sub-field value in the Sort Posts by Meta Box Group Sub Field in Bricks tutorial.

This Pro tutorial shows a relatively simplified case of the above where we want to order the rows of a Meta Box group by the value of one of its sub fields in Bricks.

Given this Meta Box group (JSON export below the screenshot):

JSON export of the field group

let's say, for a specific post the group-type field is populated like this:

With the Meta Box group query type for a query loop in the single post Bricks template like this:

The output on the single post will be:

After implementing this tutorial, it will become:

i.e., the results are sorted in ascending order of the account type (sub field value).

Solution

Add the following in child theme's functions.php (don't include the opening PHP tag in this case) 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 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags:
Pro
Post Data for the Current User in Bricks with Meta Box

Post Data for the Current User in Bricks with Meta Box

How to configure a query loop to output the post data of the SalesRep CPT associated with the current user.
Categories:
Limiting the Query Loop Posts Output to Post IDs From a Custom Field in Bricks

Limiting the Query Loop Posts Output to Post IDs From a Custom Field in Bricks

A user asks: Recently Bricks introduced the PHP query editor that lets us modify the query parameters right within the query dialog of the builder…
Categories:
Tags:
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.