putyourlightson / craft-entry-count

Counts and displays the number of times that an entry has been viewed in Craft CMS.
MIT License
47 stars 19 forks source link

Sorting entries by view count #1

Closed rickprince closed 8 years ago

rickprince commented 8 years ago

Hi Ben,

Any idea how I can sort entries by their view count? I've tried

order('count desc')

But no luck. Thanks!

putyourlightson commented 8 years ago

view count descending is the default order of the returned entries. is this not what you're seeing?

ryanfoote commented 8 years ago

Hi Ben,

When pulling entries via craft.entries, is it possible to order results by 'count'? I have the following code in a twig file:

{% paginate craft.entries.section('recipes').order('count desc').limit(9) as recipes %}

... which tosses the following SQL error:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'order clause'. The SQL statement executed was: SELECTelements.id,elements.type,elements.enabled,elements.archived,elements.dateCreated,elements.dateUpdated,elements_i18n.slug,elements_i18n.uri,elements_i18n.enabledASlocaleEnabled,content.idAScontentId,content.title,content.field_body,content.field_ctaOverride,content.field_handlingTips,content.field_ingredients,content.field_preparation,content.field_preparationTime,content.field_quote,content.field_servings,entries.sectionId,entries.typeId,entries.authorId,entries.postDate,entries.expiryDate,structureelements.root,structureelements.lft,structureelements.rgt,structureelements.level FROMelements`elements JOIN elements_i18n elements_i18n ON elements_i18n.elementId = elements.id JOIN content content ON content.elementId = elements.id JOIN entries entries ON entries.id = elements.id JOIN sections sections ON sections.id = entries.sectionId LEFT JOIN structures structures ON structures.id = sections.structureId LEFT JOIN structureelements structureelements ON (structureelements.structureId = structures.id) AND (structureelements.elementId = entries.id) WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND ((elements.enabled = 1) AND (elements_i18n.enabled = 1) AND (entries.postDate <= '2016-01-11 21:29:25') AND ((entries.expiryDate is null) OR (entries.expiryDate > '2016-01-11 21:29:25')))) AND (elements_i18n.enabled = 1)) AND (sections.handle=:sectionshandle1) GROUP BY elements.id ORDER BY count DESC LIMIT 9. Bound with :locale='en_us', :sectionshandle1='recipes'`

Clearly, the SQL error states that there is no 'count' column in the table, but figured I'd include the error for brevity.

putyourlightson commented 8 years ago

no, you'll need to use the entryCount service to fetch the entries sorted by count:

{% set countedEntries = craft.entryCount.entries %}
ryanfoote commented 8 years ago

What! I glossed right over that (multiple times) in the readme! Thanks!

Also, I think there might be an error in your code for that area of the readme. The opening of the loop should read:

{% for entry in countedEntries %}

putyourlightson commented 8 years ago

yup, fixed the typo, thanks!