Post ID Query Variable Based Post Output in Bricks

Post ID Query Variable Based Post Output in Bricks

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.
  • define a custom function 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 the specified query (Posts element or 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 custom dynamic data condition.

Step 1

Edit your "Details" Page with Bricks.

Add a Section and inside its Container, add a Heading (optional) and a Posts element or a Container having a Block if you want to use the query loop feature (on the Block).

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

Note the query loop or Posts element's Bricks ID. It will be something like tgrpze.

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

Related Tutorials..

Bricks single mode – Limiting the number of posts to 1 in the editor

Bricks single mode – Limiting the number of posts to 1 in the editor

Creating a "Single Mode" for Bricks query loops to make sure only one post is rendered in the builder.
Pro
Setting a Bricks template for Parent pages

Setting a Bricks template for Parent pages

This Pro tutorial shows how bricks/active_templates filter can be used to programmatically assign a Bricks template to all singular pages (of any post type) that…
Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories: