list categories based on updated posts [closed]

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 )
                continue;

            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;

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

                        $i++;
                    }

                }

                // 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.
                break;
            }

            if ( isset( $stop_loop ) )
                break;

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

    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:

[fresh_categories]

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.