• Resolved Andrew Steelsmith

    (@captaindrew80)


    This is my nemisis. I have an ACF relationship field called client_projects, and I just need to display the projects with the taxonomy “stage” showing “active” terms only. Can you tell me what I’m doing wrong here?

      <Loop acf_relationship=client_projects      >
        <if taxonomy=stage name=active>
      <div class="row">
          <div class="col-1"><Field acf_editor=project_pid /></div>
          <div class="col-4"><Field acf_editor=project_name /></div>
          <div class="col-2"><Taxonomy stage><Term title /></Taxonomy></div>
          <div class="col-1"><Field acf_editor=project_start /></div>
          <div class="col-1"><Field acf_editor=project_target /></div>
          <div class="col-3"><Field acf_editor=project_next_step /></div>
      </div> 
          </if>
        </Loop>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi Andrew, could you explain what you mean when you say that you want to “show ‘active’ terms only”? It seems that your issue here is that you’re not following the proper syntax for the If tag. Dynamic tags should always be capitalized (you’ve done that for your Loop but not If) and taxonomy and name are not valid attributes for the If tag. If you can elaborate a bit about what you’re trying to do, I might be able to suggest what the correct approach might be.

    At first glance, it seems like you’re trying to filter your loop to only show certain posts, so it would probably be more efficient to use some of the post loop’s query parameters to filter your query instead of nesting an If tag inside your Loop. You might write your Loop like this:
    <Loop acf_relationship=client_projects taxonomy=stage terms=exists>
    but that really depends on what you’re trying to do here when you mention showing “active” terms.

    • This reply was modified 2 years, 7 months ago by Ben @ Team Tangible. Reason: Fixed some formatting
    Thread Starter Andrew Steelsmith

    (@captaindrew80)

    Hi Ben,

    Sorry, I probably said all of that wrong. The taxonomy is called Stage, and the terms are Planned, Active, etc.

    I’m trying to filter related projects within a company to only show certain stages.

    Company (post type) > Project (post type connected by acf relationship) > Stage (taxonomy) > Active (taxonomy term)

    Ah okay, that makes sense! I actually made a mistake in my previous code example (I was trying a couple of things and ended up mashing two ideas together) but I think one solution you should try is filtering your loop using the post loop’s query parameters. So you’d use the taxonomy query parameter to only loop through posts that have that taxonomy applied and then the terms parameter to further filter your loop to only posts that have your taxonomy term “active” applied to them. So something like this:

    
    <Loop acf_relationship=client_projects taxonomy=stage terms=active>
    

    That being said, while this solution would definitely work for a regular post loop (i.e. <Loop type=post>), I’m actually not certain if the acf_relationship loop accepts the same query parameters as the regular post loop. If it doesn’t, then your first hypothesis about needing to nest some conditional logic inside your loop might actually be the right option here. If that’s the case, you’ll want to read up on what core conditions the If tag accepts and make sure your conditional logic fits that format. Something like this might work:

    
    <Loop acf_relationship=client_projects>
      <If check="{Taxonomy stage}{Term title}{/Taxonomy}" value=active>
        <div class="row">
          <div class="col-1"><Field acf_editor=project_pid /></div>
          <div class="col-4"><Field acf_editor=project_name /></div>
          <div class="col-2"><Taxonomy stage><Term title /></Taxonomy></div>
          <div class="col-1"><Field acf_editor=project_start /></div>
          <div class="col-1"><Field acf_editor=project_target /></div>
          <div class="col-3"><Field acf_editor=project_next_step /></div>
        </div> 
      </If>
    </Loop>
    

    I’m pretty certain that the second option here should work for you, but definitely give the first option a try since if the acf_relationship loop accepts regular post loop query parameters, that’d be more efficient. Note that this second option I’ve suggested would only work as-is if you only set one stage term per post, but I assume that’s the case in your example. Let me know which of these ends up working for you! If the second option ends up working for you, you could test whether it might be more efficient to Set a variable for your Term title before your conditional statement and then Get that variable both inside your If statement and inside your third div.

    Hope those ideas are enough to get this working for you, let me know if you have any other questions!

    Thread Starter Andrew Steelsmith

    (@captaindrew80)

    I tried this but I think I’m messing up the “ if check” line of your code here:

    <If check="{Taxonomy stage}{Term title}{/Taxonomy}" value=active>

    I looked for examples, but are the brackets meant to be placeholders or do I actually use them?

    That’s a really good question and isn’t something that’s particularly clear in the current documentation. To answer your question, the curly braces are supposed to be there and that’s (probably) the exact syntax you’d want to use. I’m in the process of rebuilding the docs as we speak to make that syntax clear, but for now, you can see it explained in this blog post. If you’re unclear on the correct syntax for L&L, that whole blog post might be a good read, but the short explanation is that whenever you’re writing a tag within an attribute value, you change <angle brackets> to {curly braces}. That’s always the case whether you’re just using one tag or a whole bunch of them.

    Let me know if you have any other questions about this!

    Thread Starter Andrew Steelsmith

    (@captaindrew80)

    Just circled back to this today. The second one worked! At first it didn’t, but I changed
    <If check="{Taxonomy stage}{Term title}{/Taxonomy}" value=active>

    to capitalize Active and it worked.

    You have saved me. Thank you!

    Thread Starter Andrew Steelsmith

    (@captaindrew80)

    Can I do a follow-up? Why wouldn’t the above work in an author archive?

    If you think about what your template is doing, it’s checking the current post to see if there’s a certain taxonomy term applied. This works on a singular post page because the default loop context of the page is a single post. On an archive page, the default loop context is a whole bunch of posts, so there’s no way to check something about the “current” post at the base of the archive page loop. You’d need to set things up so that your template runs once per item in the archive loop (how you might do this will depend on the builder you use) or you could use Layouts in L&L to replace an entire archive page with an L&L template. For more info about loop context, you’ll want to read this page of the docs.

    By the way, you might get better/faster support about this kind of thing on the forum at discourse.tangible.one Since it’s a more active forum with people that have a variety of expertise.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Filter ACF Relationship Field By Taxonomy’ is closed to new replies.