Feature
Post

Category
General

Last Updated Categories WordPress 2.3 Query

With Wordpress 2.3 about to be released, I had to update some code I used to correspond with the new taxonomy schema.

For another blog I run, I have a feature where I show only the categories that have recently been posted against. For example, this post is being published under the category 'Sidenotes', so sidenotes would be the first category shown on a list.

For the new schema, I had to write a new query. I thought I'd share my query in case others find it useful or want a real example of a query using the new taxonomy schema.

PHP:
  1. <?php
  2. function getLastFiveCats() {
  3.     global $wpdb;
  4.     $query = "select t.term_id as term_ids, t.name, 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.parent != 0 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 limit 5";
  5.     $results = $wpdb->get_results($query);
  6.     foreach ($results as $result) {
  7.         $catPermalink = get_bloginfo('url') . "/category/" . $result->slug . "/";
  8.         ?>
  9.             <li><a href='<?php echo $catPermalink ?>'><?php echo $result->name ?></a></li>
  10.         <?php
  11.     }
  12. }
  13. ?>

Please note that this query only searches for non-parent categories, but this can be changed to search for all categories pretty easily by removing: tx.parent != 0

Here's another query that gets all the categories and orders them by when they were last updated:

PHP:
  1. <?php
  2. function getAllCats() {
  3.     global $wpdb;
  4.     $query = "select t.term_id as term_ids, t.name, 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";
  5.     $results = $wpdb->get_results($query);
  6.     foreach ($results as $result) {
  7.         $catPermalink = get_bloginfo('url') . "/category/" . $result->slug . "/";
  8.         ?>
  9.             <li><a href='<?php echo $catPermalink ?>'><?php echo $result->name ?></a></li>
  10.         <?php
  11.     }
  12. }
  13. ?>

Recently Updated Categories

  1. By aj posted on September 20, 2007 at 5:10 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Great little code snippet. I can’t wait for Monday!

  2. By Rob posted on June 21, 2008 at 8:03 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I’m trying to write a query to retrieve all post excerpts in category id 3, but the new taxonomy and terms have me completely confused. Any help would be greatly appreciated!

  3. Trackback

    Your words are your own, so be nice and helpful if you can. If this is the first time you're posting a comment, it might go into moderation. Don't worry, it's not lost, so there's no need to repost it! We accept clean XHTML in comments, but don't overdo it please.