NO BUG: Query Loop from URL Parameters

Hi,

I have a query loop for post and I want to let it get post ID and a custom field data from a URL Parameter.

I designed a single Bricks template, had a query loop and had these meta-query

  • Meta Query for code:

  • Key: code

  • Value: {query_var:code}

  • Compare: =

  • Meta Query for date_of_valuation:

  • Key: date_of_valuation

  • Value: {query_var:date}

  • Compare: =

The URL would look like https://sample.xyz/report-summary/?code=VasL123456&date=August%202,%202024

The code there is the CPT post title.

Now, I can’t have the query display the contents.

Hi @abey,

this is possible. Please check the image below, on how I set up a meta query that checks if (my custom field) date is equal to the URL parameter value.

Note that for the URL parameter, you can use the {url_parameter} dynamic tag, where you pass the parameter name after the colon. In my case, it was named data, but in your case, it would be something like this: {url_parameter:date}.

Now, keep in mind that the URL parameter and the format in which data is saved, must be the same, so check that, please.

After you do this, it should work and that’s why I will mark it as a no bug for now.

Best regards,
M

2 Likes

Thanks.

What of the meta query for code?
The code is the title of the CPT post, but I call it code. Or should I stop using the title as code and create a CF for code?

Also, the CPT date returns the selected date in F j, Y format (August 8, 2024), this has spaces and commas which are not compatible in the URL, how will it grab this as a date?

Hey.

Now that I’m thinking, you want to show only one post, and it seems that you can control what the “code” is. So, why don’t you make that unique for each post (because it seems that you only want to get one), and then check just by that CF?
It would be much easier I suppose, because for a date, you will need to be careful, as the dates need to have the same structure as in DB and if you change one or the other, it may not work anymore.

Or maybe I’m missing something :slight_smile:

I understand, but we have to make this using parameters instead of a single post template.

It works with date, but I haven’t been able to add one more meta query.

I tried to add using the & symbol in the URL but doesn’t work. I also use the post_title and {url_parameter:code}, the code shows the post title.

I don’t have much time so may not have the full gist of you post, but since you direct messaged…

If CODE is the page title, then it isn’t meta, and won’t find anything. If you can, make it a custom field. You could write a simple function on post_save to copy the title to a custom field, so it’s never forgotten.

Otherwise - if you have the post_title - why do you need meta? Isn’t the title unique? in which case you could write a custom query using:

get_page_by_title($page_title_var, OBJECT, 'post');

If you have many posts of the same title, try using the bricks query editor and something like:

$query = new WP_Query( array(
    'post_type'  => 'post', 
    'title'      => {url_parameter:code},
    'meta_query' => array(
        array(
            'key'     => date_field_name,
            'value'   => {url_parameter:date},
            'compare' => '=', 
        ),
    ),
) );

So you can search direct on title.

Otherwise, I’d just create the whole thing as a custom query: Adding any Custom WP_Query loop to Bricks' Query Loop - BricksLabs

On a final note- it’s always easier to deal with dates as numeric (format Ymd) - so if you can do that (it’s how ACF stores dates anyway) it’ll make comparisons, and using it in searches easier.

All very much in haste… hope it helps.

2 Likes