I'm trying to save all the images related to an entry in a variable. I need the images that are added through a matrix field in the entry. I'm using the following code, but am then also getting images associated with disabled matrix blocks. I only need images from matrix blocks that are enabled.

FYI - body and builder are matrix fields and asset is the corresponding block handle.

{% set imageAssets = craft.assets().relatedTo([
  { sourceElement: entry, field: 'body.asset' },
  { sourceElement: entry, field: 'builder.asset' },
]).all() %}

Is there a way I can add conditions onto the fields that I am passing into the relatedTo parameter?


This turned out to be a Craft bug. I’ve posted an issue for it at https://github.com/craftcms/cms/issues/5849, and fixed it for the next release (3.4.12).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.