How to / examples - if tag with taxonomies

Starting a thread to share code snippets that work - starting with the IF tag and working with taxonomies. Feel free to share working examples in response.

1. If a taxonomy term exists for an existing post:

<If taxonomy=city post=current>
Do This.

This says if the current post has a city assigned to it, do something.

2. IF the value of a taxonomy field is X do something.

In this case you have to loop into the taxonomy to get the values of the fields. In my case they are custom fields.

<Loop taxonomy=city post=current>

Then you have two options:
Within the Loop:

<If field=state is="Illinois" >DO THIS.</If>

Don’t forget to close your loop.

Outside of the loop you appear to need to take a slightly different approach, using the SET command. It would look like this

<Loop taxonomy=city post=current>
  <Set name=teststate>
  <Field state />

<If check="{Get name=teststate /}" is="Illinois">DO THIS </If>

Note the is=“Yes” in the old CCS we would have to use compare=“less than” or whatever to compare the variable to the value. So you’ll have to substitute the comparison operators (at If | Tangible Loops & Logic) and swap them out for “is=”

Wow, this is awesome @dmunaretto, thanks for putting this together! Always useful to have some code examples for people to take a look at when starting out.

I thought I’d make a few clarifications about your markup for anyone else that’s looking to use your notes as a jumping-off point. I’m just a beginner with L&L myself, but I figured a second perspective couldn’t hurt.

It seems like this code is trying to use post=current as if this were a Loop tag, but post isn’t a valid attribute of the If tag (here’s the list of valid attributes). The If tag compares things based on whatever context you place it in, so if you use it in a loop, it’ll evaluate conditions based on the data from that loop. If it’s just used as-is on a post as you’re doing here, it’ll always evaluate conditions based on the current post it’s placed on. So while this markup seems to work, the post=current attribute isn’t doing anything.

We’ve already been chatting about this over in this thread but it probably bears repeating here that the correct syntax for this part would be:

<If field=state is value="Illinois">DO THIS.</If>

Yep, this is correct. If anyone wants an explanation of why this is necessary, check out this comment. The tl;dr is that post fields are different than taxonomy fields and are stored in a different part of the WordPress database, so if you want to display some taxonomy fields on a post (or use the If tag to run some conditional logic on some taxonomy fields as your code shows) you need to tell WordPress where to look to find those fields by using the `Loop’ tag.

The Get and Set tags are useful when you want to save data from a loop to be used outside the loop, as you’ve shown.

Thanks again for the thorough write-up, it’s so nice to have people like you in this little community who are willing to put in the effort to make posts like this to help others!

UGH - I can’t edit my original post now…?

Updated to reflect help from a friend… :slight_smile: I’ll try to keep adding things so that they can be corrected. For reference, we built and an entire matching engine on the CCS engine that is quite sophisticated, and are in the process of migrating it over… wish us luck!

1 Like

@dmunaretto Related to your topic: