On Facebook @Sridhar pointed me towards Brick’s bricks/query/run filter: Filter: bricks/query/run – Bricks Academy
I’m not sure how “correct” my resulting code is, but it does function as desired:
// Create custom loop to get profile reviews where "written_review" = true
// ***********************************
/* Add new query type control to query options */
add_filter( 'bricks/setup/control_options', 'profile_reviews_query_controls');
function profile_reviews_query_controls( $control_options ) {
$control_options['queryTypes']['profile_reviews_query'] = esc_html__( 'Profile Reviews Query', 'your-text-domain' );
return $control_options;
}
/* Run new query if option selected */
add_filter( 'bricks/query/run', 'run_profile_reviews_query', 10, 2);
function run_profile_reviews_query( $results, $query_obj ) {
if ( $query_obj->object_type !== 'profile_reviews_query' ) {
return $results;
}
$results = get_filtered_reviews();
return $results;
}
/* Setup loop object for reviews */
add_filter( 'bricks/query/loop_object', 'setup_profile_review_data', 10, 3);
function setup_profile_review_data( $loop_object, $loop_key, $query_obj ) {
if ( $query_obj->object_type !== 'profile_reviews_query' ) {
return $loop_object;
}
return $loop_object;
}
/* Return results from our profile reviews query */
function get_filtered_reviews() {
$filtered_reviews = [];
// Get the current post ID
$post_id = get_the_ID();
// Get the reviews repeater field
if( have_rows('reviews', $post_id) ) {
while( have_rows('reviews', $post_id) ) {
the_row();
$written_review = get_sub_field('written_review');
// Only include reviews where written_review is true
if( $written_review ) {
$filtered_reviews[] = [
'star_rating' => get_sub_field('star_rating'),
'review' => get_sub_field('review'),
'name' => get_sub_field('name'),
];
}
}
}
return $filtered_reviews;
}
/* Add profile reviews dynamic data tags */
add_filter( 'bricks/dynamic_tags_list', 'add_profile_review_tags' );
function add_profile_review_tags( $tags ) {
$tags[] = [
'name' => '{cf_reviews_star_rating}',
'label' => 'Review Star Rating',
'group' => 'Profile Reviews Data',
];
$tags[] = [
'name' => '{cf_reviews_name}',
'label' => 'Review Name',
'group' => 'Profile Reviews Data',
];
$tags[] = [
'name' => '{cf_reviews_review}',
'label' => 'Review Text',
'group' => 'Profile Reviews Data',
];
return $tags;
}
/* Render profile reviews dynamic data tags */
add_filter( 'bricks/dynamic_data/render_tag', 'render_profile_review_tags', 10, 3 );
function render_profile_review_tags( $tag, $post, $context = 'text' ) {
$loop_object = Bricks\Query::get_loop_object();
if ( !is_array($loop_object) ) {
return $tag;
}
switch ( $tag ) {
case 'cf_reviews_star_rating':
return isset($loop_object['star_rating']) ? $loop_object['star_rating'] : '';
case 'cf_reviews_name':
return isset($loop_object['name']) ? $loop_object['name'] : '';
case 'cf_reviews_review':
return isset($loop_object['review']) ? $loop_object['review'] : '';
default:
return $tag;
}
}
/* Render profile reviews dynamic data tags in content */
add_filter( 'bricks/dynamic_data/render_content', 'render_profile_review_content', 10, 3 );
add_filter( 'bricks/frontend/render_data', 'render_profile_review_content', 10, 2 );
function render_profile_review_content( $content, $post, $context = 'text' ) {
$loop_object = Bricks\Query::get_loop_object();
if ( !is_array($loop_object) ) {
return $content;
}
$tags = [
'{cf_reviews_star_rating}' => isset($loop_object['star_rating']) ? $loop_object['star_rating'] : '',
'{cf_reviews_name}' => isset($loop_object['name']) ? $loop_object['name'] : '',
'{cf_reviews_review}' => isset($loop_object['review']) ? $loop_object['review'] : '',
];
return str_replace(array_keys($tags), array_values($tags), $content);
}