1

I'm using Solspace Calendar for events, I also have a standard channel for reviews. What I need is the ability to connect the review with the event.

Solspace offers "Calendar Events" custom type field to do that, however, when I use it and attach an event to it this code:

{% set reviews = craft.entries.section('reviews').relatedTo(event) %}
{% if reviews|length %}
   {% for review in reviews %}
      <h5>{{review.title}}</h5>
      <p>{{review.description|nl2br}}</p>
   {% endfor %}
{% else %}
   <p>There are no reviews yet</p>
{% endif %}

isn't working, and I have the same code working for a standard entry. How do I use relatedTo on custom fields?

This works on a different page with entry present

{% set reviews = craft.entries.section('reviews').relatedTo(entry) %}

This doens't work when event present

{% set reviews = craft.entries.section('reviews').relatedTo(event) %}

I've made sure that:

  • the event is fetched, and I'm successfully accessing it via code above
  • there are indeed reviews with this event attached reviews in admin
1

You can use them the same way like you are used to everywhere else. All relation fields extend the same BaseRelationField so their logic is totally equal.

You can either pass an element, an element id or an ElementQuery/ElementCriteriaModel but not a string to the function. If you want to fetch all related elements of an event you'll need to fetch the event first

  • Apologies made a syntax error here on Stack, yes I'm passing the event object to the realtedTo(), the event is there and so are reviews, but they are not fetched – Lukigi Jun 20 '18 at 6:16
  • Is the code now the code you use? Because search is totally wrong. If you use Craft 2 you need to execute find() if you use Craft 3 all() – Robin Schambach Jun 20 '18 at 6:26
  • In fact, I use exactly the same code on a standard entry, as I have reviews for more than events and it works fine. – Lukigi Jun 20 '18 at 6:26
  • I've been trying with and without find() not working either – Lukigi Jun 20 '18 at 6:28
  • There must be an error in your structure or something because the code is correct. Maybe some events are disabled or don't fulfill status conditions – Robin Schambach Jun 20 '18 at 6:30
1

After experimenting with syntax a bit I made it work, looks like it needs an id, but it doesn't work with the event as an object.

So this works:

{% set reviews = craft.entries.section('reviews').relatedTo(event.id) %}

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.