I'm quite new to Craft CMS and have takeen over an application (using Twig templates). I'm trying to run a filter but not having much joy so maybe I might get some help!

So, there is a people section (craft.entries.section('people')) with over 1000 entries. These are displayed in an HTML table, each row has 3 columns (handled using the batch() call). I want to exclude certain entries that match a particular condition.

For example, I want to exclude all 'people' that partially match a list of names (e.g 'xyz', 'zzzz', 'joebloggs')

{% for people in craft.entries.section('people').order('title asc').limit(500)|batch(3) %}

Is it possible to do a query like this before the loop? Or is it something I need to check on each iteration inside the loop? Doing this might make the batch() unusable?


If you want to search for entries, you can use the search parameter:

{% set people = craft.entries
    .search('*xyz* OR *zzzz*') 

Then, you can loop through those people like this:

{% for person in people |batch(3) %}
    {{ person.name }}
{% endfor %}

By default, search doesn't do fuzzy search, that's where the *s come in. You can, however, enable fuzzy search by default.

If you want to limit your search to a field, do:

{% craft.entries.search('fieldHandle:xyz OR fieldHandle:zzzz') %}
| improve this answer | |
  • I understand that .search can be used to include those search parameters, but how would you exclude those search parameters? So you would get all entries that do NOT include xyz, zzzz. Thanks – – devwest Sep 18 '17 at 7:14
  • I'm sorry, misread your question. As mentioned on the search page of the Craft docs, I think you can exclude search using the -. So I guess that would be .search('-xyz AND -zzzz') with fuzzy search enabled, then? Not tested, though. – Paul Sep 18 '17 at 7:19
  • Hi Paul, i managed to find a way to do it by implementing the following way craft.entries.section('people').title('and, not xyz, not zzzz). – devwest Sep 19 '17 at 8:39

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.