<p>You could do something like this:</p>

<pre><code>{# Grab all entries with a body &gt; 100 %}
{% for entry in craft.entries.section('artists').order('RAND()').find() if entry.body|length &gt; 100 %}

{# Limit output to the first 4 #}
{% if loop.index1 &lt;= 4 %}
        {{ entry.id }}'s body field has a length greater than 100!&lt;br /&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>Keep in mind that if <code>entry.body</code> is a <a href="http://buildwithcraft.com/docs/rich-text-fields" rel="nofollow">Rich Text field</a> than any HTML markup (<code>&lt;p&gt;</code>, <code>&lt;span&gt;</code>, etc.) will be included in that length count as well.</p>

<p>Twig doesn't have a <code>break</code> or <code>continue</code> tag natively, but if you install the <a href="https://straightupcraft.com/craft-plugins/mn-break-and-continue" rel="nofollow">MN Break and Continue plugin</a>, you can make it a bit more efficient:</p>

<pre><code>{# Grab all entries with a body &gt; 100 %}
{% for entry in craft.entries.section('artists').order('RAND()').find() if entry.body|length &gt; 100 %}
    {% if loop.index1 &gt; 4 %}
        {% break %}
    {% endif %}

    {{ entry.id }}'s body field has a length greater than 100!&lt;br /&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-10-06T21:32:54.833Z
Thanks for this Brad. 

using this code runs through the 4 entries and only displaying the ones that have `body|length > 100`. So sometime this will only display two entries since the other two has `body|length < 100`. What I am trying to achieve is to always show 4 entries.
Kenyon Kowal
CC BY-SA 3.0
2015-10-06T21:45:20.680Z
Ahh, updated answer.
Brad Bell
CC BY-SA 3.0
2015-10-06T21:21:49.533Z
2015-10-06T21:45:15.123Z
2015-10-06T21:45:15.123Z
Brad Bell
<p>I highly recommend to use the fantastic <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow">Preparse</a> plugin to save to length of your body text in a hidden field.</p>

<p>Create and add a new "bodyLength" Preparse field to your "Artists" entry type. Use this Twig code in the field settings:</p>

<pre><code>{{ entry.body|length }}
</code></pre>

<p>To update all existing entries, re-save them by going into Settings → Sections → Artists and click Save.
You can now simply set up a criteria model to query for entries with a body length greater than 100 like so:</p>

<pre><code>{% set entries = craft.entries.section('artists').bodyLength('&gt; 100') %}
</code></pre>

CC BY-SA 3.0
2015-10-09T07:20:21.627Z
2015-10-09T07:20:21.627Z
carlcs
<p>Hey I am trying to get 4 entries, but only want 4 that have a body length > 100. </p>

<p>How would I accomplish this?</p>

<pre><code>{% for entry in craft.entries.section('artists').order('RAND()').limit(4) %}
  // only display 4 entries with entry.body|length &gt; 100
{% endfor %}
</code></pre>

<p>Thanks!</p>

<entry><sections><conditional><forloop><limit>
Only display entries based on a field conditional
# Only display entries based on a field conditional

(Asked by: username_0 on 2015-10-06)

Hey I am trying to get 4 entries, but only want 4 that have a body length > 100. 
How would I accomplish this?
{% for entry in craft.entries.section('artists').order('RAND()').limit(4) %}
  // only display 4 entries with entry.body|length > 100
{% endfor %}

Thanks!

**Tags:** entry, sections, conditional, forloop, limit


---

## 2 answers

### Answer by username_2 on 2015-10-09. Score: 4 | Accepted answer

I highly recommend to use the fantastic Preparse plugin to save to length of your body text in a hidden field.
Create and add a new "bodyLength" Preparse field to your "Artists" entry type. Use this Twig code in the field settings:
{{ entry.body|length }}

To update all existing entries, re-save them by going into Settings → Sections → Artists and click Save.
You can now simply set up a criteria model to query for entries with a body length greater than 100 like so:
{% set entries = craft.entries.section('artists').bodyLength('> 100') %}


---

### Answer by username_1 on 2015-10-06. Score: 3

You could do something like this:
{# Grab all entries with a body > 100 %}
{% for entry in craft.entries.section('artists').order('RAND()').find() if entry.body|length > 100 %}

{# Limit output to the first 4 #}
{% if loop.index1 <= 4 %}
        {{ entry.id }}'s body field has a length greater than 100!
    {% endif %}
{% endfor %}

Keep in mind that if entry.body is a Rich Text field than any HTML markup (, , etc.) will be included in that length count as well.
Twig doesn't have a break or continue tag natively, but if you install the MN Break and Continue plugin, you can make it a bit more efficient:
{# Grab all entries with a body > 100 %}
{% for entry in craft.entries.section('artists').order('RAND()').find() if entry.body|length > 100 %}
    {% if loop.index1 > 4 %}
        {% break %}
    {% endif %}

    {{ entry.id }}'s body field has a length greater than 100!
{% endfor %}

> **Comments:**
> **username_0**: Thanks for this Brad.   using this code runs through the 4 entries and only displaying the ones that have `body|length > 100`. So sometime this will only display two entries since the other two has `body|length < 100`. What I am trying to achieve is to always show 4 entries.  
> **username_1**: Ahh, updated answer.  

---

