Order Taxonomy Terms by Date Created

Quick Summary
- WordPress doesn't store when a term was created, but term IDs increase, so sorting by ID descending lists the newest terms first.
- The snippet lists service-category terms as filter items and adds a new badge to terms marked in an ACF field.
- Place the code in a theme template inside a ul element, because it outputs list items.
WordPress doesn’t store the date a taxonomy term was created, but term IDs go up as new terms are added. Sorting terms by ID in descending order therefore lists the newest terms first. This snippet uses that idea to list the terms of a custom taxonomy, service-category, as filter items, with a new badge on terms marked in an Advanced Custom Fields (ACF) field.
Place it in a theme template inside a <ul>, since it outputs <li> items.
<?php
$taxonomy = 'service-category';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => 'ID',
'order' => 'DESC',
);
$terms = get_terms( $args );
if ( $terms && !is_wp_error( $terms ) ) :
?>
<?php foreach ( $terms as $term ) { ?>
<li data-filter-class="<?php echo esc_attr($term->slug); ?>">
<?php $new_badge = get_field( 'is_new_badge_available',$term ); ?>
<?php if( $new_badge == 1 ): ?>
<img src= "<?php echo get_template_directory_uri(); ?>/assets/img/new-corner.png" alt="">
<?php endif; ?>
<?php echo $term->name; ?>
</li>
<?php } ?>
<?php endif;?>How the terms are ordered
$args is set up before get_terms() runs. 'orderby' => 'ID' with 'order' => 'DESC' puts the highest term IDs, the newest terms, first; without them the terms come back in the default order, by name. The taxonomy goes inside the same array as 'taxonomy' => $taxonomy, and get_terms( $args ) takes the whole array, which is the recommended form in current WordPress.
How the rest works
- The
ifcheck skips the list when there are no terms orget_terms()returns an error, for example when the taxonomy doesn’t exist. - Each term becomes an
<li>whosedata-filter-classattribute holds the term’s slug ($term->slug) for a JavaScript filter.esc_attr()keeps the value safe inside the attribute. get_field( 'is_new_badge_available', $term )reads an ACF True/False field on the term. Reading ACF fields from a taxonomy term covers passing the term toget_field(). When it’s on, thenew-corner.pngimage from the theme’sassets/imgfolder is shown.get_terms()skips terms without posts by default. Add'hide_empty' => falseto the arguments to list them too.
Related articles
- printing each post’s term slugs as classes: the item side of the same filter

