In the Bricks Facebook group a user asks:
Show latest 3 related posts based on category and IF THERE ARE NOT ENOUGH, fill the empty spots with latest published posts
Hi, is this possible with the query builder and how would I have to set up my query to accomplish this?
If this is not possible, showing any other posts from the same author would work too.
This Pro tutorial provides the steps to set up a Posts element or a Query Loop to fill the missing number of posts with the latest ones.
When a single post is viewed on the front end, we shall get the count of posts in the same category/categories as the current post. If the count is 3 or more, the 3 most recent of those (excluding the current one) should be output. If the count is less than 3 – let’s say there’s only 1 other post we shall output the 2 latest posts after this related post so there are always 3 posts appearing no matter what.
Bricks makes this possible via its bricks/posts/query_vars filter hook with some custom code.
Step 1
Edit your single post template with Bricks and add a related posts Section like this:

Query settings:

Do NOT add a taxonomy query here.
Step 2
Add the following in child theme‘s functions.php or a code snippets plugin:
// Use get_posts() to get up to 3 posts in the same category/categories as the current post excluding the current one.
function bl_get_posts_same_category(): array {
$args = [
'post_type' => 'post',
'posts_per_page' => 3,
'fields' => 'ids',
'category__in' => wp_get_post_categories( get_the_ID(), [ 'fields' => 'ids' ] ),
'post__not_in' => [ get_the_ID() ]
];
return get_posts( $args );
}
// Add query variables to the specified element.
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( $element_id !== 'kwtdil' ) {
return $query_vars;
}
// make the query faster
$query_vars['no_found_rows'] = true; // useful when pagination is not needed
$query_vars['update_post_meta_cache'] = false; // useful when post meta will not be utilized
// number of posts in the same category/categories as the current post
$count = count( bl_get_posts_same_category() );
if ( $count < 3 ) {
$args = [
'post_type' => 'post',
'posts_per_page' => 3 - $count,
'no_found_rows' => true,
'fields' => 'ids',
'post__not_in' => [ get_the_ID() ],
];
// merge the IDs of posts in the same category/categories as the current one + latest posts
$query_vars['post__in'] = array_merge( bl_get_posts_same_category(), get_posts( $args ) );
} else { // if the count is less than 3
// limit the posts to only those in the same category/categories as the current post
$query_vars['category__in'] = wp_get_post_categories( get_the_ID(), [ 'fields' => 'ids' ] );
}
return $query_vars;
}, 10, 3 );
Replace kwtdil with the Bricks ID of your Posts/query loop enabled element.
References
https://developer.wordpress.org/reference/functions/wp_get_post_categories/
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/