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>

@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 />

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!