19th Oct '23
/
10 comments

Querying Posts by ACF Date Sub Field in Bricks

Updated on 17 Nov 2023.

This Pro tutorial shows how we can set post__in query parameter of a query loop in Bricks to an array of post IDs for which there is at least one date custom field (inside an ACF Repeater) value that is today or in the future.

Update: We also provide the code for the case where all the dates should be today or in the future.

Consider the following scenario:

Post type: group-program (json export for import in ACF Pro)

Field group (json export):

Note that the Return Format for the date-type sub field has been set to Ymd.

Sample CPT entry with the custom field populated:

Sample entries in our test site as on 19 Oct 2023:

Program 1: 20/10/2023 (future - dd/mm/yyyy format)

Program 2: 02/10/2023 (past)

Program 3: 26/10/2023, 31/10/2023 (both future)

Program 4: (empty)

Output after implementing the tutorial:

If an entry has multiple dates, we are going to code the logic such that only current (today) or upcoming dates are output.

For example, if we change Program 3's dates to be 12 Nov 2021 and 31 Oct 2023 then the output would be:

Step 1

Let's create a custom function that loops through all the posts of our post type, gets the value of the ACF Repeater field, checks the dates of the date-type sub field for each row and if at least one date value is equal to or greater than today, add that post to the post IDs array.

Add the following in child theme's functions.php 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 625 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

How to output any post type, with the taxonomy terms as Tab titles and posts inside tables with sortable columns.
Pro
Adjacent Posts Bricks Query Loop

Adjacent Posts Bricks Query Loop

This Pro tutorial provides the steps to customize a posts query loop in Bricks to show previous and next posts on single post pages. The…
Categories:
Loop Item ID and Loop Item Object in Bricks

Loop Item ID and Loop Item Object in Bricks

If you are working on a Bricks site with specific requirements, chances are you need to grab the ID and/or object of the current item…
Categories:
Pro
Dynamic Posts Lightbox in Bricks using GLightbox

Dynamic Posts Lightbox in Bricks using GLightbox

Steps to set create a dynamic lightbox with content from the post inside of the query loop.
Categories:
Pro
Nested Queries in Bricks – Posts Grouped by Published Years and Categories

Nested Queries in Bricks – Posts Grouped by Published Years and Categories

In the past we showed how posts could be grouped by years in Bricks. This Pro tutorial takes it further by grouping posts by categories…
Categories:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
Pro
Ordering Posts by Terms in Bricks

Ordering Posts by Terms in Bricks

How to output upcoming events at the top and past events below in a single query loop in Bricks.
Categories:
Pro
CMB2 Repeatable Group Bricks Query Loop

CMB2 Repeatable Group Bricks Query Loop

This Pro tutorial provides the steps to create a custom query loop type for outputting the subfield values of a Group field (repeatable) when using…
ACF Relationship: Count of Related Posts

ACF Relationship: Count of Related Posts

Looking to get the number of CPT B posts related to a CPT A post when using a Relationship field with ACF? Here's a custom…
Categories:
Tags: