Filtering ACF Repeater Rows in Bricks by a Boolean Sub Field

Consider the following “Feature Table” ACF (Pro is needed) field group:

Sample Page with the Repeater populated:

Structure in Bricks editor:

with each column having the same ACF Repeater query loop.

After implementing the tutorial:

So essentially the requirement is to limit the ACF Repeater query loops in the three columns to only those rows for which the specified sub field – basic or business or professional is true respectively.

This Pro tutorial shows how.

Step 1

Set up the field group.

Here’s the json export from our dev site.

Step 2

Edit a post of the post type for which the field group is set. Ex.: a Page

Add your features (3 in this example) and for each select what user group/product purchase/membership etc. is associated with it.

Step 3

Edit your Page or the template with Bricks.

Copy this JSON of the fully-built section and paste.

Note: ACSS utility classes and variables are used.

Each feature table column will show all the types of features by default.

Step 4

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

/**
 * Filter the query results based on the element ID and corresponding sub-field.
 *
 * @param array $results The original query results.
 * @param object $query_obj The query object.
 * @return array The filtered results.
 */
add_filter( 'bricks/query/run', function( array $results, object $query_obj ) : array {
    $filter_map = [
        'jjkowv' => 'basic',
        'lyajtm' => 'business',
        'lwxtep' => 'professional'
    ];

    if ( ! isset( $filter_map[$query_obj->element_id] ) ) {
        return $results;
    }

    $filter_field = $filter_map[$query_obj->element_id];

    return array_filter( $results, function( $row ) use ( $filter_field ) {
        return ! empty( $row[$filter_field] );
    } );
}, 30, 2 );

Replace jjkowv, lyajtm and lwxtep with the Bricks IDs of corresponding query loop-enabled elements.

If you are trying to visualize the flow of the logic in the code, here’s a sample output of $results array:

Array
(
    [0] => Array
        (
            [feature] => Feature 1
            [basic] => 
            [business] => 1
            [professional] => 
        )

    [1] => Array
        (
            [feature] => Feature 2
            [basic] => 
            [business] => 1
            [professional] => 1
        )

    [2] => Array
        (
            [feature] => Feature 3
            [basic] => 1
            [business] => 
            [professional] => 1
        )

)