25th Jan '23
/
5 comments

Post ID Query Variable-based Post Output in Bricks

Updated on 18 Dec 2024

In the Bricks forum a user asks:

I’d like to query a single post of a custom post type using the post ID passed to a page via a URL parameter. Whats the best way to do this with Bricks?

eg

Page - /details/?post_id=123

This Pro tutorial provides the steps to

  • register post_id custom query variable.
  • use Bricks PHP query editor to check if the post_id query variable is not empty and that there is a published post having an ID equal to the query variable's value.
  • pre-filter a query loop to only show the post whose ID is set in the post_id query var (URL parameter).

After implementing the tutorial when the Details Page is visited like this:

https://example.com/details/?post_id=38

a Section having a query loop (can also be Posts element instead) will be output showing the post title and any other details pertaining to a post whose ID is 38.

If post_id is not present in the URL or if it's present but with no matching post that is published, we will set the Section to not be output by applying a dynamic data condition.

Step 1

Edit your "Details" Page with Bricks.

Add a Section and inside its Container, add a Heading (optional) and a Container having a Block to use the query loop feature (on the Block).

Click the query icon and select your desired post type (default is post).

Set Posts per page to 1.

Note the query loop element's Bricks ID. It will be something like euxiab.

Step 2

Add the following in child theme's functions.php (w/o the opening PHP tag) 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
Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

A user asks: How to Display Related Episodes Grouped by Chapters on a Podcast Page in Bricks? Hi everyone, I have two post types created…
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
Alternating Posts from Two Post Types in Bricks

Alternating Posts from Two Post Types in Bricks

How to output posts from two seperate post types, alternating in a query loop.
Categories:
Pro
Top-level Pages and Children Bricks Query Loop

Top-level Pages and Children Bricks Query Loop

This Pro tutorial for Bricks users can be considered Part 2 or a variation of the earlier Bricks Query Loop for Top-level Parent Pages with…
Categories:
Pro
Nested Queries in Bricks – Posts Grouped by Published Years and Categories

Nested Queries in Bricks – Posts Grouped by Published Years and Categories

In the past we showed how posts could be grouped by years in Bricks. This Pro tutorial takes it further by grouping posts by categories…
Categories:
Related Posts by Author in Bricks

Related Posts by Author in Bricks

Querying posts on single posts by the author of the current post.
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: