22nd Mar '23
/
2 comments

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 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Hierarchical Posts with Nested Query Loops in Bricks

Hierarchical Posts with Nested Query Loops in Bricks

Learn how to set up nested query loops in Bricks to show hierarchical posts with child posts grouped under their corresponding parent posts
Categories:
Pro
Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Rendering query-loop enabled posts depending on the value of each post's custom field value is tricky because by default, the custom field plugins' functions or…
Categories:
Pro
Upcoming Events with ACF Date Sub field Repeaters grouped by Month Years in Bricks

Upcoming Events with ACF Date Sub field Repeaters grouped by Month Years in Bricks

In the comments section of Upcoming Events Accordion in Bricks Grouped by 'Month Year' a member asked: CPT: eventACF Field: event_dates (Repeater) with event_date sub…
Categories:
Posts Related by Current Post’s Terms in Bricks

Posts Related by Current Post’s Terms in Bricks

Showing other posts assigned to the same categories/tags/custom taxonomy terms as the current post.
Categories:
Pro
Posts Grouped by Post Dates in Bricks

Posts Grouped by Post Dates in Bricks

Setting up query loops for “Post Dates” with an the inner loop displaying all posts published on that day.
Categories:
Bricks Query Loop – Posts Authored by Logged in User

Bricks Query Loop – Posts Authored by Logged in User

How to display only the posts authored by the currently logged-in user in a query loop.
Categories:
Pro
Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

How we can set a data attributes on a child taxonomy dynamically based on the parent taxonomy.
Categories:
Latest Featured Posts First in Bricks Query Loop (No Duplicates)

Latest Featured Posts First in Bricks Query Loop (No Duplicates)

In the Bricks Facebook group a user asks: We can use AI to help unfreeze our brains sometimes or at least have it write the…
Categories:
Pro
“My Favorites” in Bricks

“My Favorites” in Bricks

This Pro tutorial provides the steps to set up and use My Favorites in Bricks Builder.
Categories: