I currently have a structure called 'Divisions' that includes 'Team' pages as children. On the 'Team', there is an entries field 'Head Coach', which is an entry created on a 'Coaches' channel.

The coach has an account and has created the entry, so he is the author.

My question is, assuming the coach is the 'currentUser' is there a way to find the relation between that 'Head Coach' entry and the 'Team' if they are related by the author?

I have this code so far:

{% set coachingTeam = craft.entries.section('divisions').type('team').relatedTo({ targetElement: currentUser, field: 'headCoach' }).find() %}

The only way I've found to do this was to loop through all the teams, and if the currentUser and the author of the 'Head Coach' entry matched, then set the team.

It seems expensive, but I can't figure out how to do this with a relations call.

{% set allTeams = craft.entries.section('divisions').type('team').find() %} {% set coachingTeam = '' %} {% for team in allTeams %} {% if (team.headCoach|length and team.headCoach.first.author.id == currentUser.id) %} {% set coachingTeam = team %} {% endif %} {% endfor %}

  • Your trying to return a team that has the current user as the author and as the Head Coach?
    – aran
    Jan 12 '15 at 7:51
  • I'm trying to return the team that has the currentUser as the author of the 'Head Coach' entry that is attached to it.
    – aj1215
    Jan 12 '15 at 19:48

This is untested code.

{% set coachingTeam = craft.entries({
  section: "divisions",
  type: "team",
    { targetElement: currentUser.id },
    { targetElement: headCoach[0].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.