doublesecretagency / craft-viewcount

View Count plugin for Craft CMS
Other
6 stars 4 forks source link

Sorting entries by view count #10

Closed CreateSean closed 3 years ago

CreateSean commented 3 years ago

I just installed the view count plugin, added the counter tag to the entry template {% do craft.viewCount.sort(entries) %}, hit a few entries at random and then added this:

{% do craft.viewCount.sort(entries) %}
{% for entry in entries %}
<div class="bg-white p-4 {{ loop.last ? '':'mb-4' }}">
  {{ entry.title }}
{# more code here #}|
</div>
{% endfor %}

Then when I view a page where I'm trying to display the popular entries I get this error.

Argument 1 passed to doublesecretagency\viewcount\variables\ViewCountVariable::sort() must be an instance of craft\elements\db\ElementQuery, array given, called in /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php on line 1499

Based on the docs here: https://plugins.doublesecretagency.com/view-count/sort-by-most-viewed/ I assumed this is the correct approach.

lindseydiloreto commented 3 years ago

Make sure that entries is a query.

I'm guessing you've already applied .all()... don't do that until after you've sorted the query.

{% set entries = craft.entries.section('stuff') %} {# Don't apply .all() #}

{% do craft.viewCount.sort(entries) %}

{% for entry in entries.all() %}