Answers.Body: <p>I think something like this should work</p>
  
  <pre><code>{# First get all the stats #}
  {% set stats = craft.entries.section('statistics') %}
  
  {# iterate over those stat in a random order %}
  {% for stat in stats.order('RAND()') %}
  
      {% if stat.image | length %}
  
          {% set image = stat.image.first %}
  
      {% else %}
  
          {# If there's no image, query for a random stat, excluding those without images #}
          {% set randomStat = stats.image(':notempty:').limit(1).order('RAND()') %}
          {% set image = randomStat.first.image.first %}
  
      {% endif %}
  
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-25T16:34:25.493Z
Answers.Text: Seems like that should work, but I get this error: Impossible to invoke a method ("first") on a string variable (":notempty:").
Answers.UserDisplayName: artmem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-29T22:43:56.513Z
Answers.Text: Can you `dump` `randomStat.image` to see what's being returned there? It seems like it's not returning an asset model for some reason.
Answers.UserDisplayName: brianjhanson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-31T13:56:39.620Z
Answers.Text: When i `dump` `randomStat.image`, I get this: `string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:"`
Answers.UserDisplayName: artmem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-06T01:02:45.543Z
Answers.Text: Interesting, I'm not totally sure what's happening here. Just to confirm, what version of Craft are you using?
Answers.UserDisplayName: brianjhanson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-06T17:35:05.610Z
Answers.Text: I'm using  2.6.2981
Answers.UserDisplayName: artmem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-25T14:31:28.473Z
Answers.LastActivityDate: 2017-06-07T00:51:44.500Z
Answers.LastEditDate: 2017-06-07T00:51:44.500Z
Answers.UserDisplayName: brianjhanson
Body: <p>I have a section with an text field and an image field. the asset field may sometimes be left blank. in this case, in my template, i want to grab the image from another random entry.</p>
  
  <p>This is the code I'm trying to use, but when the image field is blank in my initial entry, I'm getting an error that Variable "image" doesn't exist.
  Am I calling the random image incorrectly?</p>
  
  <pre><code>{% for stat in craft.entries.section('statistics').limit(1).order('RAND()') %}
  {% if stat.image|length %}
      {% set image = stat.image.first() %}
  {% else %}  
      {% for random in craft.entries.section('statistics').limit(1).image(':notempty:').order('RAND()') %}
          {% set image = random.image.first() %}
      {% endfor %}
  {% endif %}
  
  {{ image.url('medium') }}')
  
  {% endfor %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-22T23:38:36.633Z
Comments.Text: Is there a reason you can't only get stats with images? Something like
  `{% set stat = craft.entries.section('statistics'.limit(1).order('RAND()')).image(':notempty') %}`
Comments.UserDisplayName: brianjhanson
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-24T00:10:00.093Z
Comments.Text: That would be ideal, but there are way more stats than images, so in this case it's not realistic for the client to require an image for every stat.
Comments.UserDisplayName: artmem
Tags: <entries><image><forloop><random>
Title: Get an image from a random entry
ThreadText: # Get an image from a random entry
  
  (Asked by: username_0 on 2017-05-22)
  
  I have a section with an text field and an image field. the asset field may sometimes be left blank. in this case, in my template, i want to grab the image from another random entry.
  
  This is the code I'm trying to use, but when the image field is blank in my initial entry, I'm getting an error that Variable "image" doesn't exist.
  Am I calling the random image incorrectly?
  
  ```
  {% for stat in craft.entries.section('statistics').limit(1).order('RAND()') %}
  {% if stat.image|length %}
      {% set image = stat.image.first() %}
  {% else %}  
      {% for random in craft.entries.section('statistics').limit(1).image(':notempty:').order('RAND()') %}
          {% set image = random.image.first() %}
      {% endfor %}
  {% endif %}
  
  {{ image.url('medium') }}')
  
  {% endfor %}
  
  ```
  
  **Tags:** entries, image, forloop, random
  
  > **Comments:**
  > **username_1**: Is there a reason you can't only get stats with images? Something like `{% set stat = craft.entries.section('statistics'.limit(1).order('RAND()')).image(':notempty') %}`  
  > **username_0**: That would be ideal, but there are way more stats than images, so in this case it's not realistic for the client to require an image for every stat.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-05-25. Score: 2
  
  I think something like this should work
  
  ```
  {# First get all the stats #}
  {% set stats = craft.entries.section('statistics') %}
  
  {# iterate over those stat in a random order %}
  {% for stat in stats.order('RAND()') %}
  
      {% if stat.image | length %}
  
          {% set image = stat.image.first %}
  
      {% else %}
  
          {# If there's no image, query for a random stat, excluding those without images #}
          {% set randomStat = stats.image(':notempty:').limit(1).order('RAND()') %}
          {% set image = randomStat.first.image.first %}
  
      {% endif %}
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Seems like that should work, but I get this error: Impossible to invoke a method ("first") on a string variable (":notempty:").  
  > **username_1**: Can you `dump` `randomStat.image` to see what's being returned there? It seems like it's not returning an asset model for some reason.  
  > **username_0**: When i `dump` `randomStat.image`, I get this: `string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:" string(10) ":notempty:"`  
  > **username_1**: Interesting, I'm not totally sure what's happening here. Just to confirm, what version of Craft are you using?  
  > **username_0**: I'm using  2.6.2981  
  
  ---
  
  