I mean to order the of categories should be based on the updated posts. The category which has most recent post should come up first and so on. Suppose, I have 50 categories and i add new post very frequently. So, From that basis I need to list 5 recently posted tagged to category. I ended up to this:

<?php wp_list_categories('number=15&show_count=0&orderby=post_date&order=rand&title_li=&hierarchical=0&taxonomy=category') ?>

WordPress, by default, doesn't keep in database values for the last time a taxonomy term was updated.

So, in order to achieve your goal, one solution is to loop through your posts and order them by date or by last modified. During the loop process you can store in a variable the term/category assigned to the post. At the end u will have as many "fresh categories" as you want.

In the code below I'm using 'product' post type and 'product_cat' taxonomy. U might want to change them to 'post' and 'category', respectively.

function display_terms_with_fresh_posts() {

    $out = '';

    $args = array(
        'post_type' => 'product', // CHOOSE YOUR POST TYPE!!!
        'posts_per_page' => -1,
        'order' => 'DESC',
        'orderby' => 'date' // Use 'date' to get the last created or 'modified' to get last updated.

    $q = new WP_Query( $args );

    // Set the var which will keep the wanted terms.
    $fresh_terms = array();

    // Set the counter which will help us to get just as many categories as we want.
    $i = 0;

    if ( $q->have_posts() ) :
        while ( $q->have_posts() ) : $q->the_post();

            $post_id = get_the_ID();
            $taxonomy = 'product_cat'; // CHOOSE YOUR TAXONOMY!!!

            $post_terms = get_the_terms( $post_id, $taxonomy );

            if ( !$post_terms )

            foreach ( $post_terms as $t ) {
                if ( !in_array( $t->term_id, $fresh_terms ) ) {

                    // Here we choose how many categories to display. In this case I chose 5, that's why the var $i needs to be at least greater than 4.
                    if ( $i > 4 ) {
                        $stop_loop = true;

                    else {
                        $fresh_terms[] = $t->term_id;



                // IMPORTANT! If u want to get all the terms assigned to each post u need to remove break from here.
                // This means that using break we will get just one term for each post.

            if ( isset( $stop_loop ) )

        $out = __( 'No posts for this post type were found.' );

    if ( $fresh_terms ) {

        foreach ( $fresh_terms as $term_id ) {
            $term_data = get_term( $term_id );
            $term_link = get_term_link( $term_id );

            $out .= "$term_data->name";


    return $out;


add_shortcode( 'fresh_categories', 'display_terms_with_fresh_posts' );

Now that u added your shortcode to display your "fresh categories" you can print the categories the way u want by using it in your pages in the admin area:


If u want to use it within php file, just do:

echo do_shortcode("[fresh_categories]");

Pay atention at the comments I left along the code snippet. It will help you in case u want to modify it a bit.
  • Good answer. One limitation or complication which may or may not matter to the questioner is that the single terms that will be found in your first alternative will by default be the first alphabetically of multiple terms by name, since the output of get_the_terms() is ordered that way, from the default arguments for wp_get_object_terms(), which get_the_terms() relies on. The output can be ordered in various ways, though obviously order would not matter if the user is following a strict one-category-per-post rule – CK MacLeod Dec 10 '16 at 8:20
  • Hi filipecsweb. Thank you for this but it only shows like this without term link. Example:(AppleBatCatDog). Can you let me know how to make spaces and get link of taxonomy as well? And also I use taxonomy images. so I want that image to appear just before the respective terms. My taxonomy images uses this: print apply_filters( 'taxonomy-images-queried-term-image', '' ); @filipecsweb – Ben Green Dec 10 '16 at 9:43
  • Hi. As u can see above, the code is getting the term link through get_term_link() function. If u say it's not working I'd say you might have a problem the way your taxonomy was registered. I didn't understand what u said about images. U tried to say that each one of your terms/categories has an image assigned to it, is that it? – filipecsweb Dec 10 '16 at 16:30
  • @filipecsweb I want to add some custom shortcodes to be displayed before every listed custom taxonomy from that. How do I do that? – Ben Green Dec 10 '16 at 21:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.