5th Nov '24
/
3 comments

Filtering Posts of a Bricks Query Loop by Meta Box Group’s Subfield Value

Updated on 8 Nov 2024

In the comments section of Sort Posts by Meta Box Group Sub Field in Bricks tutorial, a user asked:

That worked wonderfully! Is there also a tutorial how to show past events when the event year is in a group? Also how to show only future events in this scenario? : )

Consider this scenario:

CPT: event

Field group:Meta Box Group-type field: event_group|__ Number-type subfield: event_year

Test data:

When querying the event post type, all the event posts will be output by default:

The objective is to output only the events that are in the past.

i.e., as of 2024:

or only future/upcoming events:

This is not straightforward or doable via the built-in query editor controls due to the fact that the event year post meta is inside a Meta Box group.

This Pro tutorial shows how bricks/query/result Bricks filter can be used to limit the posts output by a Posts query loop based on the value of a Meta Box group's subfield value.

Update 1: Code for the case when the subfield type is Datepicker has been added near the end.

Step 1

Set up a query loop to output Events post type.

Add Heading/Basic Text etc. elements inside the query loop-enabled Block and map them to your post data like post title and the year subfield value.

Step 2

Add the following in child theme's functions.php (w/o the opening PHP tag) 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 627 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Outputting an Element in Bricks x times where x is a custom field value

Outputting an Element in Bricks x times where x is a custom field value

Learn how to output a query loop item(s) as many times as the value of a custom field in Bricks.
Categories:
Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories:
Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

In the past we showed how a Bricks posts query loop can be filtered by Meta Box group's subfield of types text/number and Datepicker here.…
Categories:
Filtering Breadcrumbs in Bricks

Filtering Breadcrumbs in Bricks

Using the bricks/breadcrumbs/items filter to modify Bricks' breadcrumbs’ element programmatically.
Categories:
Pro
Dynamic Remaining Posts Count for Load more Button in Bricks

Dynamic Remaining Posts Count for Load more Button in Bricks

A user asked in the Bricks Facebook group: I have a loop of a CPT, showing 10 in a total of 16 posts and a…
Pro
ACPT Gallery Field Query Loop in Bricks

ACPT Gallery Field Query Loop in Bricks

How we can output images from ACPT‘s Gallery field for posts as a grid using a query loop.
Categories: