How to Order Categories by Most Recently Updated
15th Jan 2011 | Posted by reza | 19 CommentsWordPress 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.
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.
- How to Show Home Link In wp_nav_menu Default Fallback Function
- Remove Title Attribute from WordPress Menu Link
- Exclude Pages or Posts of Certain Categories from WordPress Search Result
- Get Image URL of WordPress Post Thumbnail Feature
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 (+)-
-
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..
-
then how to give the current active category css for this snippet?
Trackbacks/Pingbacks
- How to Order Categories by Most Recently Updated | WebDevKungfu
- How to Add New Class to First Post in The Homepage
- Shun the Plugin: 100 WordPress Code Snippets from Across the Net | WordPress, Multisite and BuddyPress plugins, themes, news and help – WPMU.org
- 70 Excellent WordPress Tips And Tutorials From First Three Months Of 2011 | stylishwebdesigner
- 50 Unique and Informative wordpress tutorial showcase - WordPress Vampire
- 50 Unique and Informative wordpress tutorial showcase - tripwire magazine
- Wordpress - Mengurutkan Kategori Berdasarkan Kategori Postingan Terbaru - Tutorial Web Design
- 100 WordPress Code Snippets from Across the Net « Tech Snippets
- Wordpress - Mengurutkan Kategori Berdasarkan Kategori Postingan Terbaru | Tutorial Web Design
Thanks…Good…I love u Reza :-*