4
{% for entry in craft.entries.section('entertainmentEvents').status('live','pending').search('entertainmentBanner:*').order('RAND()').limit(3) %}

This is working to show 3 random entries from the full list of all entries with the entertainmentBanner field populated. How would I limit it to the first ten entries by entry date and then 3 at random from within them?

6

You could use Craft's shuffle() function to randomize your postDate ordered array of entry models:

{% set entries = craft.entries({
    section: 'entertainmentEvents',
    entertainmentBanner: ':notempty:',
    status: 'live, pending',
    order: 'postDate desc',
    limit: 10
}) %}

{% set entries = shuffle(entries) %}

{% for entry in entries|slice(0, 3) %}
    ...
{% endfor %}
| improve this answer | |
  • Damn, beat me to it! I'd update your answer to show how you would limit your 'entries' to 3? – Jamie Wade May 5 '15 at 15:05
  • Ouups, totally missed that part. Thanks Jamie. – carlcs May 5 '15 at 15:06
  • @JamieWade carlcs is an answer ninja. – Brad Bell May 5 '15 at 19:05
  • @BradBell I'll be on my guard next time. – Jamie Wade May 6 '15 at 10:15
  • Perfect. And in production already. Thanks @carlcs – Tim Print May 6 '15 at 19:10

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.