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

Related Tutorials..

Pro
Making a Nav Menu Item Active for Custom Post Type Single Views in WordPress

Making a Nav Menu Item Active for Custom Post Type Single Views in WordPress

Consider this scenario: CPT: project Page titled "Project" is being used to show a list/grid of all the projects rather than the CPT archive. When…
Categories:
How to Add a Custom Control Field to an Element in Bricks

How to Add a Custom Control Field to an Element in Bricks

In this tutorial, we'll learn how to add a custom control field to an element and dynamically change a CSS property inside the builder. Introduction…
Categories:
Tags:
Pro
Accordion Grid Query Loop in Bricks

Accordion Grid Query Loop in Bricks

How to set up a accordion in Bricks such that the "titles" are arranged in a grid with the full width content opening below.
Categories:
Pro
Restrict Content Pro in Bricks

Restrict Content Pro in Bricks

This Pro tutorial provides the steps to restrict Pages and individual elements like Sections in Bricks depending on whether the current user has any active…
Pro
“Truncate text to these many characters” Bricks Control

“Truncate text to these many characters” Bricks Control

Bricks provides a :<number> dynamic data tag modifier that can be used to limit the amount of text by the specified number of words. Ex.:…
Pro
Filtering a Bricks Terms query on Term archive pages

Filtering a Bricks Terms query on Term archive pages

On product category archives, how to show product type names but only those that the current product category has.