Order ACF Relationship by Post Term Meta

Hi,

I’m trying to order Posts from an ACF Relationship by a custom field named info_type_priority (number, which is located in the Posts (unique) Taxonomy term.
It seems difficult given the Post term meta is obviously unknown before the loop, and ordering must be done in the loop…

I tried to nest 2 loops and paste the meta value from the 1st to the 2nd loop, it returns nothing:

<Loop acf_relationship=project_outputs >

  <Set local=priority>
    <Taxonomy information_type>
      <Field info_type_priority />
    </Taxonomy>
  </Set>

  <Loop acf_relationship=project_outputs orderby={Get local=priority} order=asc>
    <Get local=priority /> - <Field Title /><br>
  </Loop>

</Loop>

Any help is welcome. :wink:

1 Like

Said differently:

  • I’m displaying Posts set from ACF Relationship field
  • Each Post has a term from the information_type Taxonomy
  • Each term has an ACF Number named info_type_priority
  • I want to order the Posts by this ACF Number

Wow, that’s a fascinating puzzle you’re trying to solve! You might want to check out this thread for a partial answer. In that case, the data by which they wanted to sort the loop was located in the post itself, so I thought the solution would be to pass the post IDs directly to a post loop and then use the post loop’s query parameters to do the sorting.

Your situation is more complicated because the data is two levels deep (for each item in the relationship field loop, you want to dig into the post and then dig further into the taxonomy term applied to it). As far as I can tell, the current implementation of the sorting features in L&L allows you to sort based on a field value, but it doesn’t allow you to sort based on a set of instructions, which is what you’d need to achieve this kind of thing.

I’ve been thinking about this all weekend and here’s what I’m currently thinking. You could create a taxonomy term loop sorted by your custom field and then within that, display a post loop of all the posts within your relationship field that have the current taxonomy term applied. I don’t have a test site with a data structure that would allow me to test something like this so I’ve written this blind, but I’ve come up with the template below to express what I’m thinking. Let me know if it works or if I missed something.

<Set relationship_array><Field project_outputs /></Set>

<Loop type=taxonomy taxonomy=information_type orderby_field_number=info_type_priority>
  <If loop exists type=post id="{Get relationship_array}" taxonomy=information_type terms="{Field name}">
    <Field title /> <!-- This displays the term title but you could also display the current priority number -->
    <Loop>
      <Field title /> <!-- This is displays the titles of all the posts within that term -->
    </Loop>
  </If>
</Loop>

There may be a better way of achieving this, but that’s what I could come up with. Let me know if that makes sense. I’d be curious to know if anyone can come up with anything better!

Taking another look at this, I realized that you could probably use the taxonomy term loop’s post parameter to get rid of that If tag from my example above.

<Set relationship_array><Field project_outputs /></Set>

<Loop type=taxonomy taxonomy=information_type post="{Get relationship_array}" orderby_field_number=info_type_priority>
  <Set priority><Field info_type_priority /></Set>
  <Loop type=post id="{Get relationship_array}" taxonomy=information_type terms="{Field name}">
    <Get priority /> - <Field Title /><br />
  </Loop>
</Loop>

Hi @benjamin,

Thanks a lot for your research.
Sorry i didn’t find the time to test them, i will keep you informed obviously.

1 Like