Get Ads with an Element Query

In addition to the prepackaged displayAd and randomizeAdGroup functions, it's also possible to collect a set of ads through an Element Query.

{% for ad in'rightSidebar').all() %}

    {# Do something creative with Ads #}
    {{ ad.displayAd() }}

{% endfor %}


{% set ads ='rightSidebar').orderBy('RAND()').all() %}

This is the same basic mechanism which allows you to get entries, assets, and categories in Twig. And just as you would with entries, you can apply "all", "one", or "ids" to the end.

# .onlyValid()

By default, an Element Query for Ads will return all matching results, whether the Ads are valid or not. However, it's possible to filter out all invalid Ads by simply chaining the .onlyValid() command anywhere in your query.

{% set ads ='rightSidebar').onlyValid().all() %}