18th Nov '24
/
0 comments

Ordering Posts by Terms in Bricks

In the Bricks Facebook group a user asks:

Hello everyone. I have a CPT 'events' and a taxonomy 'event-did-it-happen' in which there are two items 'past' and 'upcoming'.

I need to list all the 'events' posts sorted by 'event-did-it-happen' in a loop so that they are 'upcomming' first and then 'past'.

I have created a query that works for me, but as soon as I use it in Bricks as a custom query, the posts are sorted randomly.

How to go about it? Thank you

This Pro tutorial shows how to output upcoming events at the top and past events below in a single query loop in Bricks.

Consider this scenario:

CPT: EventTaxonomy: Event StatusTerms: Upcoming, Past

Default output:

After implementing the tutorial:

Note: There is no 'Event Date' custom field in this example. Within each taxonomy group (Upcoming and Past), posts will be set to be shown in descending order of published date.

If you want to instead order events based on the Event Date field automatically without manually assigning them 'Upcoming' or 'Past' Event Status taxonomy terms, follow the Custom SQL Ordering for Sorting Events in Bricks tutorial instead.

Step 1

Enable query loop on a Block.

Click the query icon and select your Event post type.

Enable PHP query editor and paste:

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
CMB2 Repeatable Group Bricks Query Loop

CMB2 Repeatable Group Bricks Query Loop

This Pro tutorial provides the steps to create a custom query loop type for outputting the subfield values of a Group field (repeatable) when using…
Pro
ACF Post Object Query Loop inside a Posts Query Loop in Bricks

ACF Post Object Query Loop inside a Posts Query Loop in Bricks

How to loop through a CPT and within each post, output a list of related posts.
Categories:
Pro
Parent Query Loop Object – Traversing Nested Query Loops Up in Bricks

Parent Query Loop Object – Traversing Nested Query Loops Up in Bricks

Bricks enables us to build nested query levels of unlimited levels. Here are a couple of examples: Sometimes, you might want to obtain the parent…
Categories:
Pro
Alternating Posts from Two Post Types in Bricks

Alternating Posts from Two Post Types in Bricks

How to output posts from two seperate post types, alternating in a query loop.
Categories:
How to show data of a post whose ID is the value of a URL parameter in Bricks

How to show data of a post whose ID is the value of a URL parameter in Bricks

In the Bricks Facebook group, a user asks: Hey Everyone,I am trying to create the following:1- I have a Custom Post Type2- I have a…
Categories:
Pro
Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering a query loop to output only those posts with the value of a Select subfield of a Group field set to Yes when using…
Categories:
Tags:
Pro
Recently Viewed Posts Bricks Query Loop

Recently Viewed Posts Bricks Query Loop

How to output the most recent X number of posts (any post type) viewed by the current visitor.
Categories: