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?

1 Answer 1

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 %}
5
  • Damn, beat me to it! I'd update your answer to show how you would limit your 'entries' to 3?
    – Jamie Wade
    Commented May 5, 2015 at 15:05
  • Ouups, totally missed that part. Thanks Jamie.
    – carlcs
    Commented May 5, 2015 at 15:06
  • @JamieWade carlcs is an answer ninja.
    – Brad Bell
    Commented May 5, 2015 at 19:05
  • @BradBell I'll be on my guard next time.
    – Jamie Wade
    Commented May 6, 2015 at 10:15
  • Perfect. And in production already. Thanks @carlcs
    – Tim Print
    Commented May 6, 2015 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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