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

Related Tutorials..

Pro
Excluding Duplicate Posts from Query Loops in Bricks

Excluding Duplicate Posts from Query Loops in Bricks

How to ensure that posts are not duplicated when two query loops are used on the same page.
Categories:
ACF Checkbox Items with Custom SVG Bullets in Bricks

ACF Checkbox Items with Custom SVG Bullets in Bricks

We walk through outputting list items with SVG icons based on the choice of a ACF Checkbox field.
Categories:
Tags:
Pro
Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

This Pro tutorial provides the steps to modify a ACF Repeater query loop to only output the rows for which a True / False type…
Categories:
Pro
Non-empty events sorted by event date and other posts below in Bricks

Non-empty events sorted by event date and other posts below in Bricks

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…
Categories:
Pro
Masonry Accordion Query Loops in Bricks Builder

Masonry Accordion Query Loops in Bricks Builder

Learn how to enable masonry layout for nestable accordions in Bricks builder and use JavaScript to re-layout the masonry on accordions' open and close.
Categories:
Tags:
How to create a dynamic infinite logo slider with ACF and Bricks

How to create a dynamic infinite logo slider with ACF and Bricks

This tutorial provides the builder settings, PHP & CSS codes that can be pasted in order to create an infinite logo slider in pure CSS…
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Update on 8 Sep 2024: You may want to follow the newer version that considers accessibility. Looking to make a Horizontal Image Accordion dynamic? This…
Pro
Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

How we can set a data attributes on a child taxonomy dynamically based on the parent taxonomy.
Categories: