How to pull ACF fields based on a taxonomy term value?

I have ACF fields that I want to output conditionally based on a taxonomy.

Here is my loop:

<Loop taxonomy=listing-type post=current>
  <If field="title" value="Lot">
      <Field name=listing_lot_size /> <If field="listing_lot_size" value="1">Acre<Else />Acres</If>
  <Else />
    <Field name=listing_bedrooms /> BD | <Field name=listing_bathrooms /> BA | <Field name=listing_sqft /> SQFT

The If statement for the tax term of Lot display this / else display that works. But I can’t pull the ACF values of the post?

Aka, that is outputting:




But not the ACF values for each…

I assume this is because I’m inside the taxonomy loop at this point, and those acf fields aren’t on the taxonomy, they are on the post.

But how do I do an If statement of a taxonomy and then show the ACF fields based on that taxonomy?

I should also clarify that this tax loop is already inside of a post loop (hence the post=current)

This is correct. Understanding loop context is an important part of L&L and it’s explained in detail in this post. It’s not explained quite as explicitly in the docs although the docs do mention “If the Field tag is placed inside a loop, it gets a field value from the current item in the loop.”

In the blog post that I linked above, you’ll want to check out the section called “How can data be passed from the inner content of one loop to the inner content of another?”

Basically, you can set some variables in one loop context and then evaluate them in another loop context. That’s probably the simplest way based on the template you already have. I imagine the most efficient way would be to use If loop exists like <If loop exists taxonomy=listing-type post=current field="title" field_value="Lot"> since that way you can check whether a loop exists in which the title of a term is “Lot” without actually entering that loop and changing the loop context, so you wouldn’t have to set any variables.

Great. I knew the problem, just not how to solve it. I’ll give it a go :raised_hands:t2:

1 Like

Yup, that totally worked to do the <If loop exists... instead of the loop itself. Great call. There was no need to do the loop, just needed to know what was inside of it :+1:t2:

1 Like

Is this also supposed to work then?

<If loop not_exists taxonomy=listing-type post=current field="title" field_value="Lot">

Because it isn’t for me on second template template I’m working on

Never mind. I just read the attribute combo of → not loop exists on the doc you linked to

Seems like not_exists should do the same… Maybe a feature in the future?

I know I’ve chatted with Eliot about this but I can’t seem to find the conversation thread. I’m pretty sure not is a sort of “logic inverter” where it would take the logic and make true logic false and false logic true. So effectively, not exists and not_exists were aliases for each other. I think the idea was to make either syntax work but I’m not sure if that ever got implemented. I’ll see if I can find the conversation and I’ll make sure there’s a feature request about it to ensure that it stays on the radar.

1 Like

Yeah, I assumed they would be aliases of each other since the logic is the same (at least verbally). Thanks for the bump on the feature request in the meantime.

Did this ever get implemented?

Doesn’t look like it. I think the consensus was that it might just make more sense to make the not attribute the main way to negate/flip logic, which would certainly make the list of comparisons on the If tag documentation page a lot more concise instead of specifying a something_not for every something comparison. The order of attributes doesn’t matter anyway in HTML/L&L, but having that as a separate attribute gives a bit more flexibility in the “phrasing” of the logic statement so that the person writing it can make it more easily readable.

Yeah, I like that solution :+1:t2: Simplifies the syntax you have to remember