Updated on 15 Jan 2024
Consider a scenario where you are showing posts from multiple post types namely post, event and game in a single query.
Also, events have an event_date custom field created using ACF.

By default, all the posts are ordered by date published in descending order.
Ex.:

Notice how there’s no event date set for Event 6.
Requirement: Exclude the empty events and order the non-empty events by event date at the top, with the rest of the items of other post types below.
Like this:

This Pro tutorial shows how advanced meta queries using named clauses can be written and used in Bricks.
Here’s how
Select the element on which query loop is enabled.
Select the post types in the query settings.

Turn on “Query editor (PHP)”.
Paste:
<?php
return [
'meta_query' => [
'relation' => 'OR',
// select posts that have event_date meta
'has_event_date' => [
[
'key' => 'event_date',
'compare' => 'EXISTS',
'type' => 'DATE',
],
[
'key' => 'event_date',
'value' => '',
'compare' => '!=',
],
],
// select non-event posts
'not_an_event_post' => [
'key' => 'event_date',
'compare' => 'NOT EXISTS',
],
],
'orderby' => [
'not_an_event_post' => 'DESC',
'date' => 'DESC',
],
];