I actually found this code on here Search Form with Craft Commerce. I had a problem with this code pulling up products that didn't match, and then after creating a new product it worked for a bit and then broke and continued to only pull up the same ones, even if I got rid the values inside with[].

So two questions, how do I enforce that the product has to match BOTH criteria in the with[] and does anyone have any clue about why it would stop refreshing products? I've tried clearing cache in both browser and craftcms.

I apologize if this has been asked before, I've been through multiple previously asked questions with little luck.

{% set spec = craft.request.getParam('a') %}
{% set spec2 = craft.request.getParam('b') %}
{% set category = craft.categories.group('productSpecs') %}
{% set products = craft.commerce.products({
            relatedTo: category,
             with: [
            order: "title asc",
        }) %}
{% for product in products %}
    {{ product.title }}
{% endfor %}

1 Answer 1


I think you're misunderstanding what with does. Or I am. Ha!

From what I understand with is for eager loading, but doesn't control anything in the relation itself. I don't think your relation makes any sense either because all you'll do is substitute a string into your spec variables.

Try something more like:

{% set products = craft.commerce.products({
    relatedTo: [
    with: [
    order: "title asc",
}) %}

And your conditionals should be something like:

{ targetElement: category },
{ sourceElement: craft.entries.section('reviews').status(null) }

The bottom example here is what you're needing: https://craftcms.com/docs/relations

  • 2
    I am with you Matt, with is for eager loading related elements. And before implementing it, @DarrylHardin needs to get the relatedTo param setup correctly for his needs. A good blog post on that topic that I can recommend: webstoemp.com/blog/combined-searches-and-filters-craft-cms
    – carlcs
    Commented Jul 6, 2017 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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