Meta Box Group Bricks Query Loop Filtered by a True/False Subfield

In a project I am working on, the requirement is to show job positions but only those with a ‘job opening status’ set to Show.

Positions: A cloneable Group-type field
Job Opening Status: A Switch-type subfield

If there are no open jobs a separate text should appear.

When the Meta Box group is selected as the query type for the Bricks query loop, it outputs all the rows, including those for which the Job Opening Status subfield is false. This Pro tutorial shows how bricks/query/result filter can be used to limit the Meta Box group field’s rows based on a boolean subfield value i.e., only show the open job positions.

Also, since the bricks_query_count dynamic data tag is not going to work for conditional checks due to use of the filter, we shall define a custom function to output either the jobs container or a fallback container conditionally.

Field group (for a specific Page in this example):

Page being edited:

Editor:

Front end:

When there’s at least open job:

When there are no open jobs:

Step 1

In the Bricks editor set up a query loop and set the query type to your Meta Box group.

Add elements inside and map their text/URL values to subfield values using Bricks’ dynamic data tags.

Step 2

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

<?php

add_filter( 'bricks/query/result', function( $result, $query_obj ) {
    if ( $query_obj->element_id !== 'vdxbbx' ) {
        return $result;
    }

    // print( "<pre>" . print_r( $result, true ) . "</pre>" );

    // Filter to include only the rows for which job_opening_status is 1.
    $result = array_filter( $result, function( $item ) {
        return isset( $item['job_opening_status']) && $item['job_opening_status'] == 1;
    });

    return $result;
}, 10, 2 );

function hz_get_open_positions_count(): int {
    $positions = rwmb_meta( 'positions' );
    $checkedCount = 0;

    foreach ( $positions as $position ) {
        // Field job_opening_status:
        $checkbox = $position['job_opening_status'] ?? 0;

        if ( $checkbox ) {
            $checkedCount++;
        }
    }

    return $checkedCount;
}

Replace vdxbbx with the Bricks ID of the query loop-enabled element.

Modify the field IDs in the code as appropriate.

Step 3

Whitelist the hz_get_open_positions_count function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'hz_get_open_positions_count'
  ];
} );

You should also add any other functions (native or custom) being used in your Bricks instance in addition to hz_get_open_positions_count. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 4

To output an element only when there’s at least 1 open job position, apply this dynamic data condition:

{echo:hz_get_open_positions_count}

To output an element only when there are 0 open job positions, apply this dynamic data condition:

You may also be interested in