Problem with Loop, Logic, and ACF Relationships

I continue to have big problems with the Logic tag. While my other post remains unsolved, I am trying to create a Logic for ACF relationship fields.

Here’s what happens:

Let’s say that I have a custom post type called day.
Every day has an ACF relationship field named lesson.
A lesson relationship field points to another custom post type called discipline.
Each discipline custom post type has, of course, a title field (and various other fields).

So, for example, I have a custom post called Monday and a custom post called Latin.
In the Monday custom post, the relationship field lesson points to Latin.

Now, I want to show only the custom posts that have Latin in the discipline relationship field.
I’m using the following and expecting to receive as result: Monday.

<Set logic="disciplines_logic" all=true>
    <If acf_relationship="lesson" title="Latin">true<Else />false</If>
  </Set>

<Loop type="day">
  <If logic="disciplines_logic">
    <Field title />
   </If>
</Loop>

However, nothing gets displayed.

Either I don’t understand how to use the Logic tag (very likely), or I am doing mistakes with the syntax. In case it’s the latter, I also tried the following variant of the if condition:

<If acf_relationship="lesson" field=title value="Latin">true<Else />false</If>

This doesn’t work either.

Alternatively (and actually better), is it possible to filter a Loop based on the value of an ACF Relationship?

I would assume a syntax like this:

<Loop type="day" acf_relationship="lesson" field=title field_value="Latin">

    <Field title />

</Loop>

But it doesn’t work.

For me, it’s really critical to be able to filter loops depending on various ACF fields (especially ACF relationship fields), but I never quite understood the syntax. Even with CCS. So resorted to using Loop and If tags in sequence, like in my initial request, and that kills my website performance.

When I saw the new Loops & Logic plugin I got excited because I thought I could define the query parameters with a Logic tag and apply them to a Loop tag directly, rather than resorting to a subsequent If, but maybe I misunderstood @eliot design.

I also use ACF relationships extensively. Not sure if this approach would apply in your case, but I’m using nested loops like this:

<Loop type=day>
  [stuff about the day here]
  <Loop acf_relationship="lesson">
	[stuff about this day's lesson here]
  </Loop>
  <br>
</Loop>  

ACF generally stores relationship field input as an array of post IDs or wp_post objects. Our Loop filtering doesn’t support arrays as far as I know, and also doesn’t seem to support includes comparisons (@eliot is that correct?), so there’s no way to apply the filtering on the Loop.

You could try modifying your original example like this to make it work properly:

<Set logic="disciplines_logic" any=true>
    <Loop acf_relationship="lesson"><If field=title value=Latin>true<Else />false</If></Loop>
  </Set>

<Loop type="day">
  <If logic="disciplines_logic">
    <Field title />
   </If>
</Loop>

One point I’d like to clarify is that the result of a logic variable is calculated when the Set tag is called, not when the logic variable is accessed by the If tag. Maybe that should be emphasized in the documentation.

(I also wonder if that behavior should be changed, to calculate the result every time the logic variable is accessed. But I’m afraid that would be a breaking change, in case anyone’s code depends on the current behavior.)

Anyway, so it should work as expected if the Set tag is moved inside the Loop.

<Loop type=day>
  <Set logic=disciplines_logic any=true>
    <Loop acf_relationship=lesson>
      <If field=title value=Latin>true<Else />false</If>
    </Loop>
  </Set>

  <If logic=disciplines_logic>
    <Field title />
   </If>
</Loop>

Woof, that’s quite complex. But I think it can’t be made much shorter, because of how the condition needs to check every post’s title in the relationship field.