22nd Feb '26
/
0 comments

How to show data of a post whose ID is the value of a URL parameter in Bricks

In the Bricks Facebook group, a user asks:

Hey Everyone,
I am trying to create the following:
1- I have a Custom Post Type
2- I have a static page, and in the URL, I am displaying a URL parameter that contains the ID of the custom post type.
I want to display dynamic data (i.e. post title) on the static page, but based on the ID in the URL.
How can I achieve this result? I tried using the dynamic fields but not able to get the data based on the url parameter.

This tutorial shows how a single post (of any post type) can be output on a Page/template whose ID is taken from the URL parameter.

Consider this URL: https://example.com/sample-page/?page_id=24

We can set up a query loop on a Block element in Bricks, select the post type (post by default), set the number of posts to 1, enable no_found_rows, turn on PHP query and specify an array having the key of p and the value set to the value of the page_id URL parameter. This array will then be merged with the query loop’s array of arguments and any dynamic data like the post_title (in curly braces) inside the Block will show that specific post’s data.

Enable PHP query on the Block and paste:

replace post_id with the name of your URL parameter.

Add a Heading inside the Block and change its text to:

{post_title}

Additionally, you may want to add this dynamic data condition on the query loop-enabled Block to ensure that it gets output only if the URL parameter has a value.

Get access to all 630 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
WordPress Posts using WP REST API in Bricks

WordPress Posts using WP REST API in Bricks

The steps to display posts from any WordPress site that has WP REST API enabled in Bricks builder using a custom query type.
Categories:
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Update on 8 Sep 2024: You may want to follow the newer version that considers accessibility. Looking to make a Horizontal Image Accordion dynamic? This…
Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
Pro
Parent post Bricks query loop

Parent post Bricks query loop

Updated on 5 Feb 2024 In the BricksLabs Facebook group a user asks: Is there a way to output the data of a parent post…
Categories:
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Creating a horizontal accordion of featured images from posts output by Bricks' query loop.
Categories:
Tags:
Displaying SureCart Featured Products & Products Collections with a Query Loop

Displaying SureCart Featured Products & Products Collections with a Query Loop

We walk through how to use a query loop to display SureCart products, featured products & collections.
Categories: