I've got an entry page and at the bottom I would like to show related products and then underneath that, related offers.

As the main page entry isn't assigned to any categories I've tried to use a custom field and the search param to show 'related' entries and this works fine, BUT, when I try and make it dynamic, it doesn't pull through. I'm guessing this is some kind of parsing order and that the custom field isn't parsed before the loop is created.

This works fine:

{# To exclude certain category #}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('A1').relatedTo(categories) %}

But this doesn't when I try and make it dynamic:

{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('{{ entry.model }}').relatedTo(categories) %}

I've also tried setting the variable first but it doesn't work:

{% set range = "{{ entry.model }}" %}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search(range).relatedTo(categories) %}

Hopefully this makes sense. Still in the basics I suppose of Craft but worth the learning curve.

  • Is A1 a title field or what would be a specific field on the model?
    – mcclaskiem
    May 4 '16 at 12:42
  • Sorry, 'A1' is the model of the Audi vehicle so in this case I want to search all other offers that have 'A1' in the title e.g. 'New Audi A1...' etc.
    – JonnyT
    May 4 '16 at 12:52
  • try adding .search('title:' ~ range) where range is the variable you set however your set needs to be = entry.model not the echo syntax. This question is somewhat similar as well craftcms.stackexchange.com/questions/9301/…
    – mcclaskiem
    May 4 '16 at 12:58
  • I've been searching yesterday and today and trying loads of various ways but never saw the post above. :/
    – JonnyT
    May 4 '16 at 13:06
{% set range = entry.model %}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('title:' ~ range).relatedTo(categories) %}

If you are only searching titles I would add that in the search params as it will help the effectiveness of the search. In addition when setting a variable you do not need to use the {{}} syntax as it will automatically use the string representation of the the field if it is a text field.

  • Excellent, thanks a lot! I'll look into search filters more so I fully understand it for next time. Thanks!
    – JonnyT
    May 4 '16 at 13:07
  • This page shows the various filters and what fields can be targeted for each element type. craftcms.com/docs/searching
    – mcclaskiem
    May 4 '16 at 13:07
  • Yeah read that page but where I was going wrong was adding 'title:range' within the search brackets as opposed to ('title:' ~ range).
    – JonnyT
    May 4 '16 at 13:09
  • Just an additional observation, if all entries have a "car model" you could make that a category field allowing for a relatedTo query which is much quicker. As you get more entries search will definitely underperform compared to a relatedTo query.
    – mcclaskiem
    May 4 '16 at 13:12
  • Considered that first but the feed gets updated daily and not really sure how to import entries along with categories just yet so it seemed the way to go. There are about 180 entries at any one time so hopefully that should be OK.
    – JonnyT
    May 4 '16 at 13:22

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.