Sorting a Bricks Query Loop by Custom Field Matching the URL Parameter

A member asks:

I am looking to buy traffic from a source, and could make the process more dynamic if i could have the ads all be unique but draw from a page of listings that are created within a loop. Is it possible to make the 1st listing be pulled to the top spot by using a custom variable (text) in a subid or something similar? ie: domain.com/shop/?subid4=toaster-oven What i mean here is, subid4 is a text field assigned in metabox and the Custom post type is gifts… and there are 20 that load up in a listicle style post TIA!!

yes, that would be manually added when creating the campaign @ the traffic source!
so if niche was a skincare offer, subid8=neutrogena -- and neutrogena if possible could be a field in the cpt?

This Pro tutorial shows how we can order a Bricks query loop to output the posts that have a specific custom field (subid) whose value is equal to the URL parameter (or query string) value.

Let's consider this sample query loop at this URL: example.com/my-page/

These posts have a subid custom field and it is populated for some.

After implementing the tutorial when example.com/my-page/?subid=toaster-oven is visited:

Notice how the post having the subid custom field value of toaster-oven appears at the top.

and when example.com/my-page/?subid=neutrogena is visited, the post having the subid custom field value of neutrogena appears at the top:

Step 1

Edit a Page/template with Bricks and set up a query loop.

Do not set Order by and Order in the query settings.

Add the following 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 524 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
Posts Grouped by Authors in Bricks

Posts Grouped by Authors in Bricks

This Pro tutorial for Bricks users provides the steps to loop through users and inside that, through posts whilst setting the inner query's author parameter…
Pro
Display 3 Category-related Posts and Supplement with the Latest Published Posts if Necessary

Display 3 Category-related Posts and Supplement with the Latest Published Posts if Necessary

In the Bricks Facebook group a user asks: Show latest 3 related posts based on category and IF THERE ARE NOT ENOUGH, fill the empty…