2

Each entry in posts has a field called value. Value is a number.

For a category, how can I return the entry with the highest value?

This is not for Craft Commerce. Value represents a budget figure.

Alternatively...

How can I return the top 3 entries (based on value)?

1

Sounds like you're looking for a combination of the orderBy and relatedTo entry query params.

This may put you on the right track?

{% set topThree = craft.entries({
  section: 'posts',
  relatedTo: { targetElement: myCategory },
  orderBy: 'myBudgetValueField desc',
  limit: 3
}).all %}

Alternatively, for just the entry with the highest value:

{% set topFigure = craft.entries({
  section: 'posts',
  relatedTo: { targetElement: myCategory },
  orderBy: 'myBudgetValueField desc'
}).one %}

Of course, this depends on myBudgetValueField being a number or some other easily-sortable value. If you need to format the value, I'd recommend storing it in the unit for comparison, and then visually treating it when you output it (i.e. store 300000, even if you eventually need to output $300,000).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.