Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

This Pro tutorial covers how posts of a Bricks query loop can be filtered to show only the ones whose true / false or checkbox type of sub field, which is inside a group-type field is equal to true (1).

ACF

Add a meta query in your query loop

where my_group is the group name and featured is the field name within that group.

Meta Box

For performance reasons, Meta Box stores all sub fields of a group in a single row as a serialized string in the database. As such, it is difficult or not straight-forward to query posts by a sub field value when using MB.

So what we can do is write a function that loops through all posts and returns a list of post IDs for which a specified True / False type sub field of a specified group field is true. Then set this function as the value of the post__in query parameter.

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

function get_mb_featured_post_ids( $group_id, $sub_field_id ) {
	$featured_post_ids = [];

	$posts = get_posts( [
		'post_type' => 'post',
		'numberposts' => 100, // a large number
	] );

	foreach ( $posts as $post ) {
		$group = rwmb_meta( $group_id, [], $post->ID );

		$checkbox = $group[ $sub_field_id ] ?? 0;

		if ( $checkbox ) {
			$featured_post_ids[] = $post->ID;
		}
	}

	return $featured_post_ids;
}

In the Bricks editor set up your query loop.

Specify the post type (if other than post).

Enable PHP query editor and paste:

return [
  'post__in' => get_mb_featured_post_ids( 'my_group', 'featured' ),
  'posts_per_page' => 100, // a large number
];

where my_group is the group ID and featured is the sub field ID.

Reference

https://metabox.io/create-group-of-custom-fields-with-meta-box-group/#how-does-the-meta-box-group-save-data