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
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:
Pro
How to Modify Products Element Query in Bricks to Output Child Products

How to Modify Products Element Query in Bricks to Output Child Products

This Pro tutorial provides the steps to limit WooCommerce products output by Bricks' Products element on single product pages to only the children of the…
Pro
Masonry for Query Loop in Bricks

Masonry for Query Loop in Bricks

If you are looking to implement masonry with a 'view more' button i.e., click-type of infinite scroll follow this tutorial instead. Updated on 15 Mar…
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:
Displaying SureCart Featured Products & Products Collections with a Query Loop

Displaying SureCart Featured Products & Products Collections with a Query Loop

We walk through how to use a query loop to display SureCart products, featured products & collections.
Categories: