doublesecretagency / craft-viewcount

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

Using Order By #31

Open seandelaney opened 7 months ago

seandelaney commented 7 months ago

I'm counting views for entries. That all works fine.

My query is

{% set partsQuery = craft.entries.section('partsEntry').limit(12).with(['partImage', 'partBrand', 'partCategory']).orderBy('priorityOrder DESC, title ASC') %}

My priorities for ordering:

I have set my key to be viewedPartEntry. I have added a Field called partTotalViews and added to Part section entry type.

If I try adding orderBy('partTotalViews DESC, priorityOrder ...', I get error

Column not found: 1054 Unknown column 'partTotalViews' in 'order clause'

How can I use your plugin to order by total views too?

lindseydiloreto commented 6 months ago

Great question. Unfortunately, your options may be a bit limited here.

The way to sort by most viewed typically looks more like this...

{% set partsQuery = craft.entries
    .section('partsEntry')
    .limit(12)
    .with(['partImage', 'partBrand', 'partCategory'])
%}

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

However, that directly conflicts with the orderBy parameter. Only one of those two sort methods can be applied at a time.

Perhaps you could give your users the option to change the sort order?

Long-term, we have plans to improve queries so that what you're looking for would be possible. No ETA on that, but I'll leave this ticket open in the meantime.