26th Apr '23
/
0 comments

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 postsHi, 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

Get access to all 632 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Previous Two Posts in Bricks Query Loop

Previous Two Posts in Bricks Query Loop

This Pro tutorial shows how we can modify the parameters of a Bricks query to limit the posts to the previous two whilst cycling to…
Pro
Posts Published in the Last x Days in Bricks

Posts Published in the Last x Days in Bricks

Updated on 1 Feb 2024 This Pro tutorial shows how to limit the posts output by either the Posts element or a Query Loop in…