Craft CMS Poll plugin v1.3.0
Easily make polls in craft cms
More Craft plugins

Fetch Poll results in Twig

Get poll results in twig

For a poll, you can get basic results from a poll with craft.poll.results(poll).

You can configure the results to:

- include the participating users (or user id's only) in a poll the poll
- get the participating users by answer
- limit the users to retrieve

If you expect a large number of users to participate in the poll, and want to get the users, you might want to limit them to avoid running into memory problems. By default, users are not included in the poll results so you'd have to explicitly configure them.

Users are sorted, the user that participated most recently is the first user or user id.
Users with any status are delivered. This includes deactivated users.

Read about events if you want to cache the poll results.

{% set results = craft.poll.results(poll, {
    with_users: true,
    user_id_only: false,
    limit_users: 1000
}) %}
{# @var \twentyfourhoursmedia\poll\models\PollResults results #}

<div class="alert alert-info">
        <h5 class="card-subtitle mb-3 text-muted">{{ poll.title }}</h5>

        {# show the total poll results, and users info #}
        <p>Total submissions: {{ results.count }}</p>
                participated user ids: {{ results.userIds | join(', ') }}<br/>
                {% for user in results.users %}
                    {{ user.username }}{{ not loop.last ? ', ' }}
                {% endfor %}

        {# show the results by answer #}
            {% for answerResult in results.byAnswer %}
                    `{{ answerResult.answer.label }}`: {{ answerResult.count }} votes,
                    {% if answerResult.percent is not null %}
                        {{ answerResult.percent | round }}%
                    {% endif %}
                            user ids: {{ answerResult.userIds | join(', ') }}<br/>
                            {% for user in answerResult.users %}
                                {{ user.username }}{{ not loop.last ? ', ' }}
                            {% endfor %}
            {% endfor %}

Legacy/deprecated method for getting results

The deprecated legacy method of getting users, with a twig filter, is still supported but should not be used.

{# deprecated #}
{% set results = poll | poll_results({}) %}