5

On an entry page I search for related entries based on the tags assigned to the respective entry. The following example works:

{% set tags = entry.q_tags.first %}

{% for tag in entry.q_tags.offset(1) %}
    {% set tags = tags ~ ' OR ' ~ tag  %}
{% endfor %}

{% set entries = craft.entries.search(tags).order('score') %}

I just wonder whether there is a smarter solution?

1

I don't think the accepted answer actually works, order: 'score' only works if you are doing a search.

I've made a plugin called Similar that orders by the number of relations to other element types, like tags.

5

This should work:

{% set tags = entry.q_tags %}
{% set entries = craft.entries.relatedTo(tags).order('score').find() %}
5
  • Do you not want to pass an array in this situation? I.e. omit the .first to make {% set tags = entry.q_tags %} and then just pass that to the relatedTo param. Jul 18 '14 at 10:29
  • Didn't notice the 'first' :)
    – Paul
    Jul 18 '14 at 11:05
  • How would you stop the current entry from displaying as a related entry? Feb 23 '15 at 17:45
  • Try adding .id('not ' ~ entry.id) to your criteria.
    – Paul
    Feb 24 '15 at 8:16
  • Ordering by 'score' only works in the context of doing a search: 'something'.
    – Brad Bell
    Apr 6 '16 at 19:30
4

Here's how you would exclude the current entry from showing within that output if so desired

{% set entries = craft.entries({
  relatedTo:      tags,
  order:          'score',
  limit:           4,
  id:             'not ' ~ entry.id
}) %}
1
  • Ordering by 'score' only works in the context of doing a search: 'something'.
    – Brad Bell
    Apr 6 '16 at 19:30

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.