Display 3 Category-related Posts and Supplement With the Latest Published Posts if Necessary

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:

(ignore the Code element)

Query settings:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Instant access to 390+ Bricks code tutorials with BricksLabs Pro