How to Order Categories by Most Recently Updated

WordPress has standard functions that are part of it’s core for displaying data such as lists of blog categories or pages. To display categories, it has wp_list_categories function, where You can order the categories by ID, name, slug, count or term_group.

How to Order Category by Most Recently Updated


Ok, I want more option for ordering the categories, I want the categories ordered by most recently updated. Most recently updated I mean, category that has the latest post will display first in the list.

You may be interested in the following WordPress tips related articles as well.

Here’s the code to perform this task :



		<?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();
		?>


Put the code in wherever place You want to display this list of category in your theme

Credit:
Thanks to jahsko and MichaelH from WordPress.org

10 Comments to “How to Order Categories by Most Recently Updated”

Add Comments (+)

  1. REZA says:

    Thanks…Good…I love u Reza :-*

  2. Dee says:

    Thanks so much for this! How do we get it to show the 5 most recently updated? Mine shows the last 3 and I havent edited the code at all nor do I see a 3 anywhere in the code to edit.. :(

    • reza says:

      How many categories you have in your site?

      • Dee says:

        Right now I have 1 category 11 subcategories and 1 uncategorized so 13 in all but that will be changing alot. I started playing with the post_per_page and changed it to 40 when there were 12 categories and it showed the last 5. I added 1 more category(13) and it started showing the last 6. Is there a formula to it?

    • reza says:

      ‘posts_per_page’ => 20

      means the script get 20 latest posts and the categories used in those 20 posts

      if only 1 categories used in those posts, then the script only displays 1 categories

      I guess, your last 20 posts only using 3 categories, so only 3 categories displayed

  3. andhie says:

    then how to give the current active category css for this snippet?

Trackbacks/Pingbacks

  1. How to Order Categories by Most Recently Updated | WebDevKungfu
  2. How to Add New Class to First Post in The Homepage
  3. Shun the Plugin: 100 WordPress Code Snippets from Across the Net | WordPress, Multisite and BuddyPress plugins, themes, news and help – WPMU.org
  4. 70 Excellent WordPress Tips And Tutorials From First Three Months Of 2011 | stylishwebdesigner
  5. 50 Unique and Informative wordpress tutorial showcase - WordPress Vampire
  6. 50 Unique and Informative wordpress tutorial showcase - tripwire magazine
  7. Wordpress - Mengurutkan Kategori Berdasarkan Kategori Postingan Terbaru - Tutorial Web Design
  8. 100 WordPress Code Snippets from Across the Net « Tech Snippets
  9. Wordpress - Mengurutkan Kategori Berdasarkan Kategori Postingan Terbaru | Tutorial Web Design

Leave a Reply

 

Amazingly Beautiful WordPress Themes