Closed rickprince closed 8 years ago
view count descending is the default order of the returned entries. is this not what you're seeing?
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: SELECT
elements.
id,
elements.
type,
elements.
enabled,
elements.
archived,
elements.
dateCreated,
elements.
dateUpdated,
elements_i18n.
slug,
elements_i18n.
uri,
elements_i18n.
enabledAS
localeEnabled,
content.
idAS
contentId,
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 FROM
elements`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.
no, you'll need to use the entryCount service to fetch the entries sorted by count:
{% set countedEntries = craft.entryCount.entries %}
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 %}
yup, fixed the typo, thanks!
Hi Ben,
Any idea how I can sort entries by their view count? I've tried
order('count desc')
But no luck. Thanks!