Use of conditional on ACF Relationship Field outside of loop

Trying to do conditional OUTSIDE of the acf relationship loop - for a county to signify singular or plural.
Nothing shows up, but I’m not sure of any other way to get what I want.

If I put it inside the loop, it shows the word “add-on” after each title.

If it’s outside, then it just doesn’t get read, because there doesn’t seem to be another way to get just the title of a relational field?

available on 
<Loop acf_relationship=plan_or_add-on>
  <span class="subtitle lowercase">
    <If count more_than value="1"> & </If>
    <Field title />
add-on<If field="plan_or_add-on" count more_than value="1">s</If>

Found a solution, but wonder if there’s a cleaner way?

available on 
<Loop acf_relationship=plan_or_add-on>
  <span class="subtitle lowercase">
    <If count more_than value="1"> & </If>
    <Field title />
add-on<Loop acf_relationship=plan_or_add-on>
 <If count more_than value="1">s</If>

Hey Alex, you’re definitely on the right track with that second post! There are a couple of things you could do to make that a little more efficient.

The first thing to look into is the last subject on the If tag which “evaluates whether the current item is the last item in a loop.” Writing <If not last> & </If> after displaying your title would basically do what you currently have but it’ll look slightly nicer.

The second thing you might want to use would be the previous_total subject (also listed on the docs for the If tag linked above). That one “evaluates the total number of items in the previous loop” so it would avoid you needing to re-do your whole loop a second time as you’re currently doing. It might generally be valuable for you to read up on loop variables since that might come in handy for this kind of thing in the future too.

Let me know if those two things are helpful, I’d be curious to see what your revised template ends up looking like once you’ve implemented those :slight_smile: Let me know if you have any other questions!

:open_mouth: wow! Incredible!

<Loop acf_relationship=landing_page>
  <div class="abovetitle lowercase">available on 
   <Loop acf_relationship=plan_or_add-on>
    <If field="title" is value="Core"><b class="badge back4 color8"><Field title /></b> plan </If> 
    <If field="title" is_not value="Core"><b class="color5"><Field title /></b> add-on</If>
    <If not last> & </If>
  <If previous_total greater_than_or_equal value="2">s</If>
</div> Commerce – The Station 2023-02-04 17.09.39

So it’s almost there - but I’ve restructured it a little so how can I set that last s only if there are more than 2 "is_not ‘core’ " field values? Because sometimes it will be just ‘core plan’, sometimes will be just ‘commerce & clients’ addon, and sometimes will be like the above example.

And how can I add an “or” statement into the if logic? ie - <if is_not/is value="core" or "basic"/>

Right. Your current template checks whether there are two items in the loop, it’s not seeing how many items there are that match or don’t match a specific value.

I see two approaches you could take. The first is kinda cool and good to know, but is probably overkill in your case. It would be to use variables with the Set and Get tags. For example, you could set a variable equal to zero at the beginning of your template and then between the opening and closing If tags for your “is_not ‘core’” logic, you could use the Get tag and Math tag to increment the variable (set the variable to be equal to the variable + 1). Then at the end instead of checking the previous_total in your If tag as you’re currently doing, you could check variable=your_variable_name_here.

But that’s probably a bit much. A better way to do this would probably just be to have two loops instead of one. Your first loop could loop through items where the title field is “core” and the second could loop through items where the title isn’t “core.” You might want to read up on filtering the loop to get an idea of the different ways that you can filter your loop. In your case, you’d probably add the following parameters to your “is_not ‘core’” loop: <Loop acf_relationship=plan_or_add-on custom_field=title custom_field_compare=not custom_field_value=Core>. Then you could just leave your If previous_total tag in place since it’ll check the loop count total of the second loop.

Let me know if this helps and if you get that all working as you need it!

There are a couple of different tools built into L&L for this. In your case, I think you’re just going to want to have your value be a comma-separated list, like value="core,basic". Keep in mind that you might want to check whether things are case-sensitive based on your setup. There are lots of different comparisons available on the If tag so if you skim that list, you’ll notice that since your value is a list, you’ll probably want to use not_in.

While that’s probably your best bet in your case, the other tools for getting granular with your If tag would be the Else tag and, if ever you need to stack a bunch of logic with and/or logic, the logic variable might come in handy.

Feel free to share your final template here since it might be helpful to others! Also, if you’re finding this plugin useful or if you’re enjoying the support, you might consider posting a review of the plugin to help share the word. I think the current average rating is 4.8 stars so we’re hoping to get that up to a nice 5.0 average rating :slight_smile:

Whether you feel like doing that or not, keep your questions coming!

1 Like

So this works really well for the last half (if I’m doing two loops), except it’s not filtering out the “Core” values - just lists it anyways.

And how would I filter out to be is/not “Core, Basic” as it seems the logic doesn’t compute that (‘basic’ discolors).

<Loop acf_relationship=plan_or_add-on custom_field=title custom_field_compare=not custom_field_value=Core> 
  <Field title />
  <If not last> & </If> 
  <If last> addon<If count more_than value="1">s</If></If>

I’d absolutely leave another review if I could! This is my 5th template for this site with L&L, and probably will have 2-3 more - look forward to sharing the final result. What’s incredible for this page is I’m pulling data via the relationship field from a CPT that has even more relationships, so the parent template is quite nested. It truly lives up to it’s name - shouldn’t have slept through Logic class.

I didn’t realize you had already left a review, thanks so much! Glad to hear you’re getting a lot of value out of the plugin.

I just realized a really obvious oversight in my recommendation: you’re using an acf_relationship loop, not a post loop, so of course the query parameters available for the post loop wouldn’t work on the acf_relationship loop. Luckily, the workaround is really simple, since your relationship field <Field plan_or_add-on /> outputs an array of post IDs, we can just pass that list of IDs to the id parameter of a post loop (I’m not sure what post type(s) are in your relationship field so you’ll have to change the post type value below accordingly). So transforming your loop into a regular post loop would look something like this:

<Loop type="post,plan" id="{Field plan_or_add-on}" custom_field=title custom_field_compare=not custom_field_value=Core> 

At that point, any of the query parameters or attributes available on the loop tag should work to filter your results. Sorry for not catching this earlier!

You might try removing the space in that list to see if that fixes things. Also, you could wrap it in quotes just to be on the safe side. The fact that basic is discoloring in the code editor suggests that L&L thinks it’s a separate attribute. You can see the same thing happening in the forum’s code editor when you forget to quote a value that contains a special character like a space:

<tag attribute=value1,value2 />
<tag attribute=value1, value2 />

Not sure if that’s what your issue is here but wrapping things in quotes and removing the space might solve things for you. Also, as noted in the part of the docs about loop filtering, those custom_field... parameters are working with raw data so they’re not as flexible as other approaches. I think they should work in your case since the raw value of your title field is “Core” so it’s easy to check whether the title is or isn’t a certain value, but it’s good to be aware of the contexts in which those different loop filtering options work.

I actually meant how to how two values there - to be, I guess, custom_field_value=Core, Basic?

Going to test out the updated loop, thanks!

Exactly, syntax-wise that looks like <tag attribute="value1,value2" /> or custom_field_value="Core,Basic"

Again, you’re going to want to read that section about loop filtering since those custom_field... parameters won’t allow you to work with a value that’s a list. For that, you’ll probably want to use the field attribute since that one allows you to use all the same comparisons that are available in the If tag.

1 Like

Now I’m understanding the difference.
Is there a list of what’s possible with custom_field_compare and all possible parameters, as opposed to just field. Like the If comparisons?

I still can’t get the filtering to work properly, either with custom_field or field, trying various things.

But just using that new snippet, it shows the data when the following is removed, but if I use the filtering options, it’s not pulling up any data. That’s the correct case, I’ve tried it both lower & uppercase.

custom_field=title custom_field_compare=not custom_field_value=Core

I’ve also tried it without the compare and it still doesn’t show anything. One post for sure has just the Core page related (no other pages related).

So I’ve decided to try something else - I’ve split it into two separate fields, which has made it easier. Here’s the final template that gets the result I want:

<Loop acf_relationship=plan_or_add-on  sort_order="asc" sort_field="title">
  <Field title />
  <If not last> & </If> 
<If previous_total greater_than_or_equal value="2">plans</If>
<If previous_total is value="1">plan</If>

<Set logic=complex_condition all=true>
  <If field=plan_or_add-on exists>true<Else />false</If>
  <If field=add-on exists>true<Else />false</If>
<If logic=complex_condition>&</If>

<Loop acf_relationship=add-on  sort_order="asc" sort_field="title">
  <Field title />
  <If not last> & </If>
<If previous_total greater_than_or_equal value="2">add-ons</If>
<If previous_total is value="1">add-on</If>

Maybe there’s a cleaner way of doing this, but it covers all the possible variations.

From the docs about general attributes that work on all Loop types:

field_compare="..." - This attribute defines the comparison operator, or how the field should be compared to the field_value - See If tag: Common comparisons for more information.

And then from the docs about query parameters available specifically for the post Loop type:

custom_field_compare="..." - Compare using one of: “equal” (default), “not”, “before”, “before_inclusive”, “after”, “after_inclusive”

Glad you got your logic figured out! You mentioned that you “still can’t get the filtering to work properly.” Did you try changing your acf_relationship loop into a post loop as I’d suggested above?

You’d need to change post,plan to the name (or names) of the post types you’re referencing in your relationship field, but other than that I’d assume it should work. If not, you could replace those query parameters custom_field=title custom_field_compare=not custom_field_value=Core with this field=title field_compare=is_not field_value=Core and see if that works for you.

In any case, this is just about fine-tuning your template, but I’m glad you’ve got it all working as you need it!

Ah - I didn’t realize it’s under the Loop doc. I guess I sort of went at it backwards, from If → Loop filtering. In any case, thank you.

Yep - copy and pasted directly, changed the post type (to page), and tested it out with and without the filter queries. Worked without the query parameters, but didn’t with. I do think I tried that swap, but will keep it in mind and revisit later. Much appreciated. Again, good job on an incredible plugin!