Related Posts by Taxonomy in Bricks

In the previous Posts Related by Current Post’s Terms in Bricks tutorial, we showed how posts related to the current single post that have the same terms as the current post can be configured in a Bricks query loop.

That method is not reliable esp. in situations when the terms have commas and other special characters in them. This tutorial addresses this question a user asked in the comments section there:

I got this working on my page but for some reason it’s not working for taxonomies that have commas in them, like “Cancer, Heart Attack, and Stroke” or “Dental, Vision, and Hearing” It displays fine for other Taxonomies without commas

This Pro tutorial provides the steps to use PHP query editor of a Bricks query loop to output x number of posts (of any post type) related to the current single post by a specified taxonomy.

After implementing the tutorial you could simply paste

return bl_get_related_posts_query();

in a query loop’s PHP editor and sign code to output 5 posts that have the same category/categories as the current single post being viewed.

or for example,

return bl_get_related_posts_query( 'project', 'project-type', 7 );

to output 7 project CPT posts that have the same project-type custom taxonomy terms as the current single project being viewed.

Step 1

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

<?php

function bl_get_current_post_terms( string $taxonomy ): array {
    $post_id = get_the_ID();
    $terms = wp_get_post_terms( $post_id, $taxonomy, ['fields' => 'ids'] );
    
    return is_wp_error( $terms ) ? [] : $terms;
}

function bl_get_related_posts_query( string $post_type = 'post', string $taxonomy = 'category', int $posts_per_page = 5 ): array {
    $current_post_id = get_the_ID();
    $term_ids = bl_get_current_post_terms( $taxonomy );
    
    if ( empty( $term_ids ) ) {
        return [
            'post__in' => [ 0 ],
        ];
    }
    
    return [
        'post_type' => $post_type,
        'posts_per_page' => $posts_per_page,
        'post__not_in' => [ $current_post_id ],
        'tax_query' => [
            [
                'taxonomy' => $taxonomy,
                'field' => 'term_id',
                'terms' => $term_ids,
            ],
        ],
    ];
}

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

Step 2

Set up a query loop in a Section in your single post Bricks template.

Enable PHP query editor and paste the return statement as mentioned in the intro of this tutorial.


This solution works by:

  1. Getting the term IDs of the current post for the specified taxonomy.
  2. Using these term IDs in the tax_query, which avoids issues with commas in term names.
  3. Using ‘field’ => ‘term_id’ instead of ‘name’ in the tax_query, which is more reliable and avoids issues with special characters in term names.

This approach works regardless of whether the taxonomy terms contain commas or other special characters. It is also more efficient as it uses term IDs instead of names.