Order terms alphabetically by ACF text field & hide empty terms

This is going to be basic, but I’d appreciate some help as I’m learning the syntax. I’m displaying terms and (1) want to order them alphabetically by an ACF text field, not the term’s title; and (2) hide empty terms, i.e. those terms that were created but have not been assigned to any posts.

Here’s my attempt at ordering:

<Loop taxonomy=guest orderby_field=name_ordering>(stuff)</Loop>

What am I doing wrong? How do I hide empty terms? Thanks!

I’m not positive that the taxonomy_term Loop supports orderby_field yet. @eliot would know.

As for hiding empty terms:
<Loop type=taxonomy_term taxonomy=guest orderby=name_ordering><If loop type=post taxonomy=guest terms="{Field id}">(stuff)</If></Loop>

The if statement checks whether a post loop can be made with the current taxonomy term as a filter, and so the iteration will be hidden if there are no posts assigned to the term.

A few other notes:

  • Your original example is missing type=taxonomy_term.
  • If the custom taxonomy applies to a different post type, you’ll have to change “type=post” to “type=custom_slug”.

Thank you, @julia. This is helpful on multiple levels. @eliot, it would good to know if orderby_field works or will work with the taxonomy_term loop any time soon.

Hi @dan, thank you for the question.

In the newest version 1.2.1 of the plugin published today, the orderby_field attribute will work with the taxonomy_term loop. Previously it only worked with the post loop.

There’s also a new attribute hide_empty to hide terms that have no associated posts. It’s used like:

<Loop taxonomy=guest orderby_field=name_ordering hide_empty=true>
2 Likes

@eliot hide_empty=true is nice and simple; however, I still can’t get orderby_field to work. Here’s the code:

<Loop type=taxonomy_term taxonomy=guest orderby_field=name_ordering hide_empty=true>
  <a href="{Field url}">
    <img src="{Field acf_image=photo field=url alt='{Field title}'}"/>
    <Field title />
    <Field name_ordering />
  </a>
</Loop>

You can see it displays the content of the custom field on the front-end (for testing purposes, just the initial of their first name), but it won’t order the terms by it.

Any ideas? Thanks for your time.

I appreciate the issue description and screenshot.

i did more testing and found that the logic for orderby_field, in particular sorting by custom field, needed improvement. Along the way I found the same issue with taxonomy loop, so improved that also.

Please try updating the plugin to version 1.2.2, and see if that solves it.

1 Like

Nicely done. It works. Thank you!

Hi there,
I wonder how to use acf_select with values A to Z to get a list of posts ordered by such a selection field, e.g. a list of staff or team members.

I´ve tried orderby_field what is not working with a select and I still have the loop for an ordered list:

<Loop type=member> <Field member_name /><br> <Field member_function /><br> <Field member_phone /> </Loop>

<Loop type=member orderby_field=acf_test_sort> is working but I need an idea how to use select instead, because I want to order this way

A = an select value
member A1
member A2

B = an select value
member B1
member B2

Hi Christoph,
Could you explain what you’re trying to do in a bit more detail? Where is the value A-Z going to be selected, and which piece of data are you comparing it to on the member? If the goal is to set up frontend filtering (the site visitor can select a letter from a dropdown to limit listings to only matching ones) you’ll need to use some JS to set that up

There is a list of team members. The goal is to have a page with an alphabetically order, example

A
M. Albert / title / phone / email
T. Allister / title / phone / email

B
M. Brown / title / phone / email
T. Byron / title / phone / email

When starting with LL I´ve tried the acf_field_lastname but there is no way to get the first letter of the lastname (A, B, C) to get it in an alphabetically order sorted the way described above. So I created an acf_select (so I have 26 clear options to choose from A … Z).

Now I’m looking for an entry point into the first loop: All values from the acf_select which have an assigned employee. Show the first letter of each value. The second embedded loop should be the output of the employees (for example all employees whose last name starts with A).

There is no need to select or filter the list.

As long you have first and lastname in one field its really tricky.

If you separate first and last name, you can order by last-name and use

<Format lenght=1><Field last-name /></Format>

to get the first letter of the lastname.

Then you can set a variable to be checked if the first letter changes and print it only out if this is the case.

Or you can loop a list with all letters and then use

<Loop list=letters> or <Loop items="A,B,C....">
<Loop type=post_type field=last-name field_value="{Field}" field_compare=starts_with>
1 Like