SOLVED: Dynamic Data Doesn't Work in Query Editor (Subquery)?

Forgive me if this was already addressed somewhere, but it doesn’t seem like the Dynamic Data tags are working in the query editor. (Or at least not working as I would expect?)

I have a query that returns my products. Within that query I have the following query to return all variations:

$parent_id = {post_id};

return [
  'post_type' => 'product_variation',
  'post_parent' => $parent_id,
];

Nothing is returned. I’m not sure if the dynamic data tag {post_id} is returning nothing, or if it’s returning something other than the expected product ID from the parent product in the outer loop.

The same issue appears to be mentioned in this How To post:

1 Like

However, when I use the following in the query editor, everything works as expected:

$parent_id = get_the_ID();

return [
  'post_type' => 'product_variation',
  'post_parent' => $parent_id,
];

Hi @JS40CD,

I’ve tried to replicate it - on a single product page template, and I was unable to. It works in my case.

What you can try, is to add error_log($parent_id); right after the first line, and then you can check the result/returned value in the debug.log file.

What does it return?
Matej

Alright. I can confirm that {post_id} is returning 68, which is the ID of the store page, instead of the ID of the product. Do you need anymore information? I’m not sure where to go from here.

PS. Just to reiterate, I am using a query loop of the products on the store page, and within that I have this subquery that returns the variations of each product.

Here is the main query loop in the shop page. It returns all products. The query loop that I am having issues with (to return product variants) is inside this query loop. The main query loop settings are shown below:

And here is the structure of the two queries in the structure panel. You can see that the element Block - Products is where the main product query loop happens, shown in my last post. The query loop that returns product variants is found in the Div - Variations element.

image

And again, just to make sure it’s clear: I am expecting that when I have a subquery, dynamic data like {post_id} will return data (in this case the post ID) from the post that is returned by the main query loop, so I can use that data in creating the subquery.

Hence:

$parent_id = {post_id};

return [
  'post_type' => 'product_variation',
  'post_parent' => $parent_id,
];

should create a query loop for each product that returns the variations of those products, if there are any.

Is this perhaps not meant to work this way?

Hi @JS40CD,

aha, I see now, you are using it on the shop page. I think you need to enable the “Disable query merge” setting.

  1. Disable “Query editor (PHP)” - don’t worry, you will enable it back soon-
  2. Enable “Disable query merge”
  3. Enable “Query editor (PHP)” back.

I’ve tested those steps, and I got it working.

Let me know.
Matej

I’ve disabled the query editor on the subquery and enabled Disable Query Merge, and then enabled the query editor again. Still nothing.

I do have a work-around by using $parent_id = get_the_id();. Would you like to keep debugging or skip it?

I can send you the JSON (all elements) of the template, if that would help.

Hi,

as I can’t replicate it locally, I think it’s best if you can send a temporary login credentials to your test/staging website and a link to this topic to help@bricksbuilder.io using the email address you used during the purchase, then I can have a look.

Meanwhile, on your live website, feel free to use a workaround.

Thank you,
Matej

To my experience and analysis: Bricks tags in code are replaced AFTER parsing the PHP.

This is invalid PHP syntax and throws a fatal error:
$parent_id = {post_id};

Instead, put the tag in quotes:
$parent_id = '{post_id}';

In most cases, PHP doesn’t care about string or integer and can handle both with no issues.
In this case, for constructing a query, it’s absolutely fine to use the ID as a string.


After reading all the comments, the issue seems to be that the {post_id} tag returns the page ID instead of the product id, or you checked that outside the query editor. Not sure how you checked that.
You should add a error_log output to your query code, like:

$parent_id = '{post_id}';
error_log('Query parent_id: ' . $parent_id);

This would make sure you’re checking the actual ID and using that for your query.

Thanks for jumping in!

Yes, I used error_log, which returned the ID of the store page in the debug log file, instead of the ID of the parent product.

I just sent them my information. We’ll see if they find out if it was human error or a bug in this case.

Thank you for reporting back.
Sorry, then my assumption was not the cause for your issue.
I’ll follow this topic to learn what the actual culprit is.

EDIT: My problem is solved. I missed the ‘semi-colon’ in parent loop.

Same problem here, when dealing with nested query loops for product categories. This is the Query editor inside the main category loop and is not pulling the {term_id} and throws a fatal error. Disable query merge is turned on by the way on both these queries as the display is meant to be for mega menu.

Log returns an empty value.

Hi @khan360 ,

We are able to replicate the {post_id} issue inside the nested query loop. We are working on it. (It’s a bug that Bricks picked the wrong post context in the nested query loop before executing the nested query)

Unfortunately, I am not able to replicate the issue like yours, in a Nested Term loop when using {term_id}

Kindly create a new forum thread to describe your setup in a more complete way. (What is the parent loop setup) What is the template type. What is the Fatal error etc.

Thank you,

Regards,
Jenn

1 Like

Sorry @itchycode , for deleting my earlier comment. I managed to solve the problem. Works now. Apparently, I missed the semi-colon at the end of return statement.

1 Like

Hi guys,

We fixed this issue in Bricks 2.0 RC, which is now available in your account.
Changelog: Bricks 2.0-rc Changelog – Bricks

Please let us know if you are still experiencing issues.

As with any pre-stable release, please do not use it on a production/live website. It is only meant for testing in a local or staging environment.

1 Like