doublesecretagency / craft-viewcount

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

Unable to sort entries #28

Closed adrianjean closed 1 year ago

adrianjean commented 1 year ago

For some reason the sort function isn't working for me. Here is my setup:

Load entries from two sections, posted in the last 7 days

{% set entries = craft.entries({ 
    section:            ["article","podcastEpisode"],
    publishStatus:      "published",
    postDate:           '>= ' ~ now|date_modify('-7 days')|date('Y-m-d'),
}) %}

Sort the Entries {% do craft.viewCount.sort(entries, 'viewCount') %}

Run Query {% set entries = entries.all() %}

When I then loop over them with {% for entry in entries %} I can see the view counts, but they are not in order. It's as if the sorting doesn't happen.

Anything I am doing wrong?

ViewCount: 1.2.2 CraftCMS: 4.5.6.1 PHP: 8.2.11 MYSQL: 8.2.11

adrianjean commented 1 year ago

Here's something interesting... Instead of using:

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

and I use this:

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

I see that all the entries with "0" views are grouped together, and all the entries with 1+ views are together, but not in order.

adrianjean commented 1 year ago

Ugh... I mixed up "Field Name" for "key" in the {% do craft.viewCount.sort(entries, 'KEY') %}.

When using the key set in the Field Settings the sort works!!

lindseydiloreto commented 1 year ago

Ok cool, glad you got it all worked out! 🙂