Whoa, that’s some funky stuff you’re up to! Haha!
I can sorta see what you’re doing (thanks for simplifying this down and cutting out the extraneous bits) but I feel like I’m still a little overwhelmed by this. I’ll give it my best shot.
First, an easy improvement. In option B, you’re creating a comma-separated list of taxonomy terms. To do that, you’re overwriting a variable that includes a previously saved instance of itself and then you’re appending one more piece to it for every item in the loop. This seems inefficient to me. The way I’d approach creating a comma-separated list would be to simply move that Set
tag outside the loop. That way you could do something like this:
<Set my_list>
<Loop acf_select=my_taxonomies>
<Loop type=taxonomy_term taxonomy="{Field}_cat" post=current>
<Field name /><If not last>,</If>
</Loop>
<If not last>,</If>
</Loop>
</Set>
Probably a lot more efficient this way.
One quirk I’ve noticed about HTML (not just L&L) is that when you have things on multiple lines, sometimes spaces can get added in. Most of the time this is fine, but when working with variables it can sometimes mean that unwanted spaces end up saved inside your variable. This might not affect your particular use case, but in case it does, you could also try just compacting that down to something like this. A pretty hard to read, but it’s the exact same as above, compacted:
<Set my_list><Loop acf_select=my_taxonomies><Loop type=taxonomy_term taxonomy="{Field}_cat" post=current><Field name /><If not last>,</If></Loop><If not last>,</If></Loop></Set>
And here’s a second recommendation. In the hybrid approach at the end, I see why you approached it that way because you want to only add query parameters when they’re actually necessary since otherwise you’d just be looping through stuff you don’t need to loop through.
There are probably a few different ways to approach this. The approach that I can see would be to conditionally loop through one, two, or three taxonomy terms depending on how many are selected in that outer loop. I might get this wrong so you’ll need to sanity check whether I’ve got your data structure right, but my basic idea would be something like this:
<Loop acf_select=my_taxonomies>
<If total value=3>
<Set query=my_query type=a taxonomy=tax1_cat terms="{Get my_list}" taxonomy_2=tax2_cat terms_2="{Get my_list}" taxonomy_3=tax3_cat terms_3="{Get my_list}" taxonomy_relation=or />
<Else if total value=2>
<Set query=my_query type=a taxonomy=tax1_cat terms="{Get my_list}" taxonomy_2=tax2_cat terms_2="{Get my_list}" taxonomy_relation=or />
<Else if total value=1>
<Set query=my_query type=a taxonomy=tax1_cat terms="{Get my_list}" />
</If>
</Loop>
<Loop query=my_query>...
I’m not sure if this will increase the efficiency of your template, but it might be a different way of thinking about it that can help you figure out an approach that performs well with the current tools that exist in the language. Let me know how this goes!