How to sort your categories by most recent update

Published on: 20 November 2025. Last updated date: 20 November 2025

 

Code snippets are small pieces of code added to WordPress or WooCommerce to extend or adjust a site's behavior without requiring an additional plugin. They allow you to optimize performance, customize specific features, and maintain precise control over what is actually executed. By avoiding the burden of external modules, they reduce the risk of incompatibilities while offering great flexibility to adapt the experience to the project's needs. For a site that evolves regularly, it's a clean and controlled way to integrate targeted improvements.

This snippet allows you to automatically reorganize your categories based on their most recent update date. By retrieving the latest recorded change for each category and then sorting them accordingly, it provides a clear and dynamic view of your most active content. This approach facilitates editorial management by highlighting the most frequently updated categories, while simplifying the monitoring of sections requiring special attention.

1|Information

Before using this snippet, make sure you have installed the Code Snippets plugin (free) on your WordPress site. If not, you can download it via the link below. If you have never added a specific feature to your WordPress site, we recommend you start by reading our dedicated guide (link below).


2|Prérequis

Here is the list of extensions required for this snippet to work properly. Make sure to install them before activating the snippet.

WooCommerceNecessary for selling virtual products on your site

3|Snippet

Here is the snippet to use. The comments inside the code will guide you on how this snippet works.

Make sure to check the "Run snippet everywhere" box before activating the snippet.

/**
 * Comment classer vos catégories par ordre de mise à jour la plus récente
 */
<?php
$cat_array = array();
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 20,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$cat_args=array('orderby' => 'none');
$cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
foreach($cats as $cat) {
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
}
if ($cat_array) {
foreach($cat_array as $cat) {
$category = get_term_by('ID',$cat, 'category');
echo '<a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>'.'<br />';
}
}
wp_reset_query();
?>

Functional summary

This snippet :

  • retrieves the 20 most recent articles;
  • collects all categories related to these articles (without duplicates);
  • displays a list of links to these categories.

It doesn't explicitly sort categories by last update date, but since it's based on recent posts, it highlights recently active categories. This is a fairly common approximation: you can browse current editorial news without bothering with a more sophisticated search query.

Take it a step further

This type of customization demonstrates how WooCommerce can be extended through WordPress filters and actions. If you'd like to further customize your loyalty program, consider consulting the WooRewards documentation or hiring an experienced WordPress developer (see our Custom Development page).

Our Plugins

We have created powerful and widely acclaimed plugins for WooCommerce. Boost your sales with our solutions

WooRewards

Discover the most powerful loyalty plugin for WooCommerce. Simple or tiered systems, referrals, social networks, badges and achievements, you will find all the tools to build YOUR loyalty program

Learn More

VIP Membership

VIP Memberships is a complete membership management tool for your WooCommerce site. Sell subscriptions to your customers and offer them benefits such as preferential prices or exclusive products.

Learn More

Virtual Wallet

Offer your customers a virtual wallet on your website. Let them save money by purchasing your products and use this credit on future purchases. This extension also offers a complete gift card tool

Learn More

Referral Codes

Win new customers with this complete SEO tool. Whether through influencers or simple referrers, reward them and the new customers they bring

Learn More