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 the latest post if necessary when viewing single posts.

Array Index Post Number Sample Post ID Previous Posts
0 7 (latest) 16 [6, 5]
1 6 19 [5, 4]
2 5 24 [4, 3]
3 4 56 [3, 2]
4 3 83 [2, 1]
5 2 13 [1, 7]
6 1 (oldest) 1 [7, 6]

Examples:

When on the post with ID 56, posts with IDs 83 and 13 will be shown.

When on the latest post i.e., post with ID 16, posts with IDs 19 and 24 will be shown.

When on the very first (or oldest) post i.e., post with ID 1, posts with IDs 16 and 19 will be shown.

Step 1

Edit your single post template with Bricks and add a Section.

Inside the Section's Container, add a h2 Heading that reads say "Previous Posts".

Add a Container and inside that a Block.

Check "Use query loop" on the Block.

Click the query icon.

Set Posts per page to 100 or a large enough number that is typically higher than the number of posts in the site. For example, if the site is only going to ever have 30 posts set this to say 40 or 50.

Add your desired elements like Post Title inside the Block.

At this stage, this will output all the posts in the site.

In the next step, we shall get the post IDs of the previous 2 posts and set this as the value of post__in query parameter.

Step 2

Add this in child theme's functions.php or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 525 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.
How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories:
Pro
Bricks Query Loop – How to Insert Elements After Every Nth Post

Bricks Query Loop – How to Insert Elements After Every Nth Post

Update: Follow this tutorial instead. This Pro tutorial provides the steps to output element(s) in a Bricks' query loop after every nth post. Use case:…
Categories:
Pro
CPT Posts Grouped by Custom Taxonomy Terms in Bricks

CPT Posts Grouped by Custom Taxonomy Terms in Bricks

The steps to set up a Service Category terms loop with an inner Service posts loop in Bricks without writing code.
Categories:
Pro
Nav Menus Custom Query Types in Bricks

Nav Menus Custom Query Types in Bricks

Updated on 21 Aug 2024 This Pro tutorial shows how custom query types for each navigation menu can be generated in Bricks. This enables us…
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 1)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 1)

This tutorial provides the PHP & JS codes that can be pasted in order to create a dynamic map with markers populated from a custom…
Categories:
Pro
Events grouped by Event Date custom field in Bricks

Events grouped by Event Date custom field in Bricks

This Pro tutorial for Bricks users provides the steps to output posts of an event custom post type dynamically grouped by the value of their…
Categories: