Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

This Pro tutorial provides the steps to filter the rows of a Meta Box cloneable group to only those that have a date custom field value set to today in Bricks query loop.

Use case: Can be used when setting up a restaurant website where the recipes for only the current day should be shown on single posts.

Step 1

Install Meta Box and its AIO extensions.

Create a field group having a Date Picker type of field.

Step 2

Edit your posts and populate the field group.

In this example, today’s date is 4 Apr 2023

Step 3

Edit the single Bricks template for your post type.

Copy this Section’s JSON and paste it.

We have selected “MB Group: Post Data” as the Query Type where “Post Data” is the ID of the cloneable group.

Step 4

Add the following in child theme‘s functions.php or a code snippets plugin:

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
	if ( $query_obj->element_id !== 'undnix' ) return $results;
  
	// return only the results that have date_khpbe7p5pn Meta Box sub field value set to today's date	
	$filtered_results = array_filter( $results, function( $result ) {
		return $result['date_khpbe7p5pn'] === date( 'Y-m-d' );
	} );

	return $filtered_results;
  }, 30, 2);

Replace undnix with the Bricks ID of your query loop element.