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

Leave the first comment

 

Related Tutorials..

Pro
Custom Nested Queries in Bricks

Custom Nested Queries in Bricks

In most of the previous tutorials covering nested query loops in Bricks on this site, we accessed the parent query's looping object in the inner…
Categories:
Pro
6 Random Featured Posts in Alphabetical Order in Bricks

6 Random Featured Posts in Alphabetical Order in Bricks

How to output x number of random posts with a specific meta value set, in Alphabetical order.
Categories:
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:
Querying Posts by Date Field to Exclude Past Events with ACPT Plugin

Querying Posts by Date Field to Exclude Past Events with ACPT Plugin

In the Bricks' Facebook group, a user asked: Meta query? What am I missing?Hi all, really need a little guidance on this one. I have…
Categories:
Pro
Bricks Query Loop for Top-level Parent Pages with Children

Bricks Query Loop for Top-level Parent Pages with Children

In the Bricks Facebook group, a user asked: Looking for a custom query to output a list of posts in the same hierarchy that the…
Categories:
Pro
Posts Grouped by Month and Year in Bricks

Posts Grouped by Month and Year in Bricks

Nesting query loops with months & years in descending order, with the inner loop outputting the posts.
Categories:
How to List Your Posts Divided by Categories in Bricks

How to List Your Posts Divided by Categories in Bricks

In this tutorial, we'll learn how to display a list of posts divided by each category. The DOM tree Here is how I structured my…
Categories: