Ask your WordPress questions! Pay money and get answers fast! (more info)

How can I display category summaries according to freshness?


For a new theme, I want to display category summary boxes (1 excerpt + 3 headlines) similar to what you see in [[LINK href=""]]the center column of Mimbo Pro[[/LINK]].

But -- I want to display just 3 category summary boxes, ordered by whichever category was updated last. Can this be done?

Answers (2)


Max answers:

Hi Darren,

I think that the most efficient way of doing this is with a raw mysql query:

select wp_term_taxonomy.term_taxonomy_id from wp_posts, wp_term_relationships, wp_term_taxonomy WHERE taxonomy='category' AND wp_term_taxonomy.term_taxonomy_id=wp_term_relationships.term_taxonomy_id AND object_id=wp_posts.ID AND post_type='post' AND post_status='publish' order by post_date DESC LIMIT 20;

This query returns the last updated categories taking in consideration the last 20 post published.

Then you could select a subset of the three distinct categories at the top of this result set and run a loop for each of them. You know best:


Hope this help.
Happy new year!



Tom Ransom answers:

I can't take credit for the code (see: [[LINK href=""]]Last Updated Categories WordPress 2.3 Query from Devlounge[[/LINK]] )


function getAllCats() {
global $wpdb;
$query = "select t.term_id as term_ids,, t.slug, max(p.ID) as id, tx.count from $wpdb->terms t, $wpdb->term_taxonomy tx, $wpdb->term_relationships tr, $wpdb->posts p where t.term_id = tx.term_id and tx.taxonomy = 'category' and tr.term_taxonomy_id = t.term_id and tr.object_id = id and p.post_status = 'publish' group by term_ids order by id desc";
$results = $wpdb->get_results($query);
foreach ($results as $result) {
$catPermalink = get_bloginfo('url') . "/category/" . $result->slug . "/";
<li><a href='<?php echo $catPermalink ?>'><?php echo $result->name ?></a></li>