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

Related Tutorials..

Pro
Upcoming Events in Bricks Grouped by ‘Month Year’ with Frontend Filtering by Event Type

Upcoming Events in Bricks Grouped by ‘Month Year’ with Frontend Filtering by Event Type

Updated version of the earlier Upcoming Events Grouped by ‘Month Year’ in Bricks tutorial with some improvements:
Pro
Post Views Counter Query Loop in Bricks

Post Views Counter Query Loop in Bricks

How we can output the most viewed posts in your Bricks site in a query loop.
Categories:
Pro
Current Post’s Terms in Bricks

Current Post’s Terms in Bricks

Updated on 15 Jan 2024 This Pro tutorial provides the steps to set up a grid of terms (like categories) that the current post is…
Pro
Parent post Bricks query loop

Parent post Bricks query loop

Updated on 5 Feb 2024 In the BricksLabs Facebook group a user asks: Is there a way to output the data of a parent post…
Categories:
Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories: