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
<?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 if check skips the list when there are no terms or get_terms() returns an error, for example when the taxonomy doesn’t exist.
  • Each term becomes an <li> whose data-filter-class attribute 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 to get_field(). When it’s on, the new-corner.png image from the theme’s assets/img folder is shown.
  • get_terms() skips terms without posts by default. Add 'hide_empty' => false to the arguments to list them too.
Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More