It is a common requirement to show posts (posts/CPT items) that belong to the same taxonomy (category/tag/custom taxonomy) as the current single post.
This Pro tutorial shows how this can be done in Bricks taking the example of testimonial Custom Post Type and testimonial_type as the taxonomy.
We are going to
- exclude the current single testimonial post from being output in the related testimonials list.
- add “s” at the end of “Related Testimonial” heading when there is more than one related testimonial.
- ensure that the Section gets output only if there is at least one related testimonial.
to limit the posts to those that belong to the same taxonomy term as the current single testimonial post.
Step 1
In your single post/CPT Bricks template, add a Section.
Inside the Section’s Container, add a Heading that reads something like “Related Testimonial”.
Add a Posts element or set up a Query Loop.
Set your post type, the number of posts to be pulled and tick “Exclude current post”.

Step 2
Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:
// Limit the testimonial CPT posts to those that belong to the same testimonial_type taxonomy term as the current single testimonial post
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( 'ikxbpy' === $element_id ) {
$testimonial_types = get_the_terms( get_the_ID(), 'testimonial_type' );
$testimonial_types_ids = wp_list_pluck( $testimonial_types, 'term_id' );
$query_vars['tax_query'] = array(
array(
'taxonomy' => 'testimonial_type',
'terms' => $testimonial_types_ids,
),
);
}
return $query_vars;
}, 10, 3 );
// Function to return the count of testimonial CPT posts that belong to the same testimonial_type taxonomy term as the current single testimonial post
function bl_get_testimonial_count() {
$testimonial_types = get_the_terms( get_the_ID(), 'testimonial_type' );
$testimonial_types_ids = wp_list_pluck( $testimonial_types, 'term_id' );
$testimonials = new WP_Query( array(
'post_type' => 'testimonial',
'post__not_in' => array( get_the_ID() ),
'tax_query' => array(
array(
'taxonomy' => 'testimonial_type',
'terms' => $testimonial_types_ids,
),
),
) );
return $testimonials->found_posts;
}
// Add "s" at the end of a specific heading element's text if there is more than 1 related (by taxonomy) post.
add_filter( 'bricks/element/settings', function( $settings, $element ) {
if ( $element->id === 'gavtcq' && bl_get_testimonial_count() > 1 ) {
$settings['text'] .= 's';
}
return $settings;
}, 10, 3 );
Replace ikxbpy with the Bricks ID of your Posts element or the element on which Query Loop is being applied.
Replace all instances of testimonial_type with the name of your taxonomy.
Enter your post type in
'post_type' => 'testimonial',
Replace gavtcq with the Bricks ID of your heading element.
Step 3
Add a Dynamic data condition on the Section.
{echo:bl_get_testimonial_count}
>
0
