14th Nov '23
/
0 comments

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

Related Tutorials..

Pro
Posts Published in the Last x Days in Bricks

Posts Published in the Last x Days in Bricks

Updated on 1 Feb 2024 This Pro tutorial shows how to limit the posts output by either the Posts element or a Query Loop in…
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…
Pro
Non-empty events sorted by event date and other posts below in Bricks

Non-empty events sorted by event date and other posts below in Bricks

Updated on 15 Jan 2024 Consider a scenario where you are showing posts from multiple post types namely post, event and game in a single…
Categories: