I am trying to build a query to fetch some entries which I want filter on a table field between multiple values. The table field is "filterTestWidth" and has 1 col named "size" (number type). There are multiple entries with multiple values in col1.

{% set items = craft.entries({
    "section": "catalog",
    "filterTestWidth": [
        "and",
        "> 180",
        "< 190"
    ]
}) %}

Unfortunate the Craft manual is limited on this subject. Does anyone know a more detailed article about element criteria?

up vote 0 down vote accepted

Table fields in Craft just store their data in a JSON blob in the content table. That means you can't really query them like you would other things, you'd have to load in all of the data, and then filter it out as an array.

While you can certainly do this in Twig, it smacks of a bit too much "business logic" in Twig; so at this point I might be considering one of two options:

  1. Refactoring it out to a plugin or module that will do the heavy lifting behind the scenes

  2. Changing the schema I'm using to store the data in something that can be queried, such as moving the data into entries or a matrix block or perhaps a SuperTable

Or if either of these seems like too much work, or you're not storing a whole lot of data in the Table field, then I'd just do it in Twig by looping through all of the rows in the table, and selecting only the ones that meet my criteria.

  • Thanks for the clarification. I have used super tables before, great plugin! Do you know a good example or an article about filtering with supertables? – RvdB Aug 12 at 19:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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